|
1 | 1 | package bonuses.english; |
2 | 2 |
|
3 | 3 | import java.util.List; |
| 4 | +import java.util.function.IntPredicate; |
4 | 5 |
|
| 6 | +import bonuses.teachingmaterial.Combining; |
5 | 7 | import engine.Locale; |
6 | 8 | import engine.Sensei; |
7 | 9 | import sensei.AboutInterfacesKoans; |
@@ -54,7 +56,7 @@ public class AboutInterfaces { |
54 | 56 | * // Since Sword implements Weapon, it must implement the hit method. |
55 | 57 | * @Override // This special annotation means the method is defined elsewhere (our interface in this case) |
56 | 58 | * public void hit(Monster monster) { |
57 | | - * // some code computing and applying damage to the monster, applying some tear and wear on the weapon, etc... |
| 59 | + * // Some code computing and applying damage to the monster, applying some tear and wear on the weapon, etc... |
58 | 60 | * } |
59 | 61 | * |
60 | 62 | * } |
@@ -91,6 +93,173 @@ public class AboutInterfaces { |
91 | 93 | * |
92 | 94 | */ |
93 | 95 |
|
| 96 | + |
| 97 | + /** |
| 98 | + * # Anonymous interface implementation |
| 99 | + * |
| 100 | + * Write a method 'getAnonymousCombining' which returns an anonymous implementation of 'bonuses.teachingmaterial.Combining'. |
| 101 | + * The implementation of the combine() method should return the second number subtracted from the first. |
| 102 | + * |
| 103 | + * --------- TIPS -------------- |
| 104 | + * |
| 105 | + * Sometimes, it is tedious to create a class file to implement an interface just for one occasion. |
| 106 | + * In these situation, you can implement the interface in an anonymous class. It is anonymous, because it does not have a name. |
| 107 | + * That class is instantiated immediately where it is created. For example: |
| 108 | + * |
| 109 | + * public Weapon toss() { |
| 110 | + * return new Weapon() { |
| 111 | + * @Override |
| 112 | + * public void hit(Monster monster) { |
| 113 | + * // Some code computing and applying damage to the monster, applying some tear and wear on the weapon, etc... |
| 114 | + * } |
| 115 | + * } |
| 116 | + * } |
| 117 | + * |
| 118 | + * When looking at this code, you could be tempted to believe there is a constructor for the interface Weapon, but there is not. |
| 119 | + * We are really creating a class, for which there exists a single object. |
| 120 | + * The constructor with empty parameters is the one of this nameless class. |
| 121 | + * We can now get and use the tossed weapon this way: |
| 122 | + * |
| 123 | + * Weapon tossedWeapon = toss(); |
| 124 | + * tossedWeapon.hit(zombie); |
| 125 | + * |
| 126 | + * ------------------------------- |
| 127 | + * |
| 128 | + * Expected result: |
| 129 | + * |
| 130 | + * getAnonymousCombining().combine(3, 4) should return -1 |
| 131 | + * |
| 132 | + */ |
| 133 | + |
| 134 | + |
| 135 | + /** |
| 136 | + * # Lambda methods |
| 137 | + * |
| 138 | + * Write a method 'getLambdaCombining' which returns an lambda method implementing 'bonuses.teachingmaterial.Combining'. |
| 139 | + * The implementation of the combine() method should return the first number subtracted from the second. |
| 140 | + * |
| 141 | + * --------- TIPS -------------- |
| 142 | + * |
| 143 | + * When an interface have only one method, there is an even shorter form to implement it. You can use what is called a 'lambda method'. |
| 144 | + * A 'lambda method' is a stripped down version of a method. Since our example interface 'Weapon' has only a single method 'hit()', we can use this shortcut: |
| 145 | + * |
| 146 | + * For example: |
| 147 | + * |
| 148 | + * public Weapon toss() { |
| 149 | + * return (monster) -> { |
| 150 | + * // Some code computing and applying damage to the monster, applying some tear and wear on the weapon, etc.. |
| 151 | + * }; |
| 152 | + * } |
| 153 | + * |
| 154 | + * We can now get and use the tossed weapon this way: |
| 155 | + * |
| 156 | + * Weapon tossedWeapon = toss(); |
| 157 | + * tossedWeapon.hit(zombie); |
| 158 | + * |
| 159 | + * The general syntax for lambda method returning void, or having a body with multiple lines: |
| 160 | + * |
| 161 | + * ([param1Name], [param2Name], ...) -> { |
| 162 | + * // Lambda method body here |
| 163 | + * } |
| 164 | + * |
| 165 | + * If your lambda is having a single expression, you can even skip the parentheses and the 'return': |
| 166 | + * |
| 167 | + * ([param1Name], [param2Name], ...) -> // expression here |
| 168 | + * |
| 169 | + * Here are some example of methods and their lambda equivalent (assuming the interface has only one method in it): |
| 170 | + * |
| 171 | + * This interface implementation: |
| 172 | + * |
| 173 | + * public void sayHello() { |
| 174 | + * System.out.println("hello"); |
| 175 | + * } |
| 176 | + * |
| 177 | + * Can be replaced by this lambda: |
| 178 | + * |
| 179 | + * () -> { |
| 180 | + * System.out.println("hello"); |
| 181 | + * } |
| 182 | + * |
| 183 | + * This interface implementation: |
| 184 | + * |
| 185 | + * public int square(int x) { |
| 186 | + * return x * x; |
| 187 | + * } |
| 188 | + * |
| 189 | + * Can be replaced by this lambda: |
| 190 | + * |
| 191 | + * (x) -> x * x |
| 192 | + * |
| 193 | + * This interface implementation: |
| 194 | + * |
| 195 | + * public int min(int x, int y) { |
| 196 | + * if (x < y) { |
| 197 | + * return x; |
| 198 | + * } |
| 199 | + * return y; |
| 200 | + * } |
| 201 | + * |
| 202 | + * Can be replaced by this lambda: |
| 203 | + * |
| 204 | + * (x, y) -> { |
| 205 | + * if (x < y) { |
| 206 | + * return x; |
| 207 | + * } |
| 208 | + * return y; |
| 209 | + * } |
| 210 | + * |
| 211 | + * ------------------------------- |
| 212 | + * |
| 213 | + * Expected result: |
| 214 | + * |
| 215 | + * getLambdaCombining().combine(3, 4) should return 1 |
| 216 | + * |
| 217 | + */ |
| 218 | + |
| 219 | + |
| 220 | + /** |
| 221 | + * # Common lambda interfaces |
| 222 | + * |
| 223 | + * Write a method 'getIsEven' which returns a lambda method testing if an integer is even. |
| 224 | + * |
| 225 | + * --------- TIPS -------------- |
| 226 | + * |
| 227 | + * Since lambda methods are so useful, a lot of interfaces already exist in the Java standard library, and we don't have to create them ourselves. |
| 228 | + * |
| 229 | + * For example: |
| 230 | + * |
| 231 | + * For a lambda taking no parameter, and returning nothing, {@link java.lang.Runnable}: |
| 232 | + * |
| 233 | + * Runnable sayHello = () -> { System.out.println("Hello"); }; |
| 234 | + * |
| 235 | + * For a lambda taking a int parameter, and returning nothing, {@link java.util.function.IntConsumer}: |
| 236 | + * |
| 237 | + * IntConsumer displayInt = (anInt) -> { System.out.println(anInt); }; |
| 238 | + * |
| 239 | + * The same exist for other type. For example {@link java.util.function.DoubleConsumer}: |
| 240 | + * |
| 241 | + * DoubleConsumer displayDouble = (aDouble) -> { System.out.println(aDouble); }; |
| 242 | + * |
| 243 | + * The reverse functions, taking nothing as a parameter, but returning something exist as well: {@link java.util.function.IntSupplier}, {@link java.util.function.DoubleSupplier}. etc... |
| 244 | + * |
| 245 | + * DoubleSupplier giveMePiPleeeaaase = () -> 3.14159; |
| 246 | + * |
| 247 | + * There is also a lot of case where you would need to test a number somehow. This is where interfaces like {@link java.util.function.IntPredicate} shine: |
| 248 | + * |
| 249 | + * IntPredicate isPositive = (number) -> number >= 0; |
| 250 | + * |
| 251 | + * For the exercise, you can use the modulo operator, %, which computes the remainder of an integer division: |
| 252 | + * |
| 253 | + * int remainder = 17 % 5; // remainder equals 2 |
| 254 | + * |
| 255 | + * ------------------------------- |
| 256 | + * |
| 257 | + * Expected result: |
| 258 | + * |
| 259 | + * getIsEven().test(4) should return true |
| 260 | + * |
| 261 | + */ |
| 262 | + |
94 | 263 |
|
95 | 264 | public static void main(String[] args) { |
96 | 265 | new Sensei(Locale.en, List.of(AboutInterfacesKoans.koans)).offerKoans(); |
|
0 commit comments