1 file changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -57,4 +57,33 @@ in books or on the internet.
57
57
class Order {
58
58
Cart cart;
59
59
}
60
- ```
60
+ ```
61
+ 1. it's often helpful to use currying(https://github.com/mtumilowicz/groovy-closure-currying)
62
+ and functional interfaces to design API
63
+ ```
64
+ @FunctionalInterface
65
+ interface CurrableDoubleBinaryOperator extends DoubleBinaryOperator {
66
+
67
+ default DoubleUnaryOperator rate(double u) {
68
+ return t -> applyAsDouble(t, u);
69
+ }
70
+ }
71
+ ```
72
+ then we can easily implement conversion classes
73
+ ```
74
+ class RateConverter implements CurrableDoubleBinaryOperator {
75
+
76
+ @Override
77
+ public double applyAsDouble(double value, double rate) {
78
+ return value * rate;
79
+ }
80
+
81
+ static DoubleUnaryOperator milesToKmConverter() {
82
+ return new RateConverter().rate(1.609);
83
+ }
84
+
85
+ static DoubleUnaryOperator celsiusToFahrenheitConverter() {
86
+ return new RateConverter().rate(1.8).andThen(x -> x + 32);
87
+ }
88
+ }
89
+ ```
0 commit comments