Type classes example in Java. Inspired by Scala example (slides), Typeclasses
- In Haskell, typeclass is the language feature.
- In Scala, typeclass is a pattern.
- In Java, typeclass is a experiment.
A type class is a type system construct that supports ad hoc polymorphism. This is achieved by adding constraints to type variables in parametrically polymorphic types. Such a constraint typically involves a type class T and a type variable a, and means that a can only be instantiated to a type whose members support the overloaded operations associated with T. Wikipedia
In this code Scala implicits are replaced by Spring DI. Case classes are created with Lombok @Value and matched by javaslang-match.
- Calculate final checkout
- Calculate average orders price
- Orders::average through Stats::mean
- DRY principle
- Separation of concerns
- Evaluation is delegated to OrderEvaluation and ProductEvaluation
- Decouple functionality form types
- Simple and clean domain model
- [Single responsibility] (https://en.wikipedia.org/wiki/Single_responsibility_principle) * additional domain features are delegated to specialized classes (Stats and Evaluable implementations)
- ./gradlew test