3
3
import java .math .BigDecimal ;
4
4
import java .util .function .Supplier ;
5
5
6
- public class Mixins {
6
+ public class Traits {
7
+
8
+ public static void main (String [] args ) {
9
+ var ecorp = new ECorp ();
10
+ report (ecorp );
11
+ }
7
12
8
13
/*
9
14
* DOWNSIDES
@@ -21,7 +26,7 @@ public class Mixins {
21
26
* - utility methods
22
27
*/
23
28
24
- public void report (Megacorp megacorp ) {
29
+ public static void report (Megacorp megacorp ) {
25
30
// without `var` there would be no way to declare a variable
26
31
// of type `IsSuccessful` and `IsEvil`
27
32
var corp = (MegacorpDelegate & IsSuccessful & IsEvil ) () -> megacorp ;
@@ -46,6 +51,26 @@ interface Megacorp {
46
51
47
52
}
48
53
54
+ static class ECorp implements Megacorp {
55
+
56
+ @ Override
57
+ public String name () {
58
+ return "E-Corp" ;
59
+ }
60
+
61
+ @ Override
62
+ public BigDecimal earnings () {
63
+ // "One. Million. Dollars"
64
+ return new BigDecimal ("1000000" );
65
+ }
66
+
67
+ @ Override
68
+ public BigDecimal taxes () {
69
+ return BigDecimal .ONE ;
70
+ }
71
+
72
+ }
73
+
49
74
// created right next to `Megacorp` to allow easy extension
50
75
// throughout the system
51
76
@ FunctionalInterface
@@ -77,11 +102,11 @@ default BigDecimal taxes() {
77
102
// these are concepts that are only useful in a very narrow part of the system
78
103
// and so they are not added to the original interface to prevent polluting it
79
104
// with too many methods;
80
- // these mixins must not have abstract methods
105
+ // these traits must not have abstract methods
81
106
82
107
interface IsSuccessful extends Megacorp {
83
108
84
- final BigDecimal SUCCESS_BOUNDARY = new BigDecimal ("500000000" );
109
+ BigDecimal SUCCESS_BOUNDARY = new BigDecimal ("500000000" );
85
110
86
111
default boolean isSuccessful () {
87
112
return earnings ().compareTo (SUCCESS_BOUNDARY ) > 0 ;
0 commit comments