Merge pull request #314 from iluwatar/fstrategy
Functional approach to Strategy pattern #310
This commit is contained in:
commit
4e7a8fdaca
@ -5,6 +5,10 @@ package com.iluwatar.strategy;
|
||||
* The Strategy pattern (also known as the policy pattern) is a software design pattern that enables
|
||||
* an algorithm's behavior to be selected at runtime.
|
||||
* <p>
|
||||
* Before Java 8 the Strategies needed to be separate classes forcing the developer
|
||||
* to write lots of boilerplate code. With modern Java it is easy to pass behavior
|
||||
* with method references and lambdas making the code shorter and more readable.
|
||||
* <p>
|
||||
* In this example ({@link DragonSlayingStrategy}) encapsulates an algorithm. The containing object
|
||||
* ({@link DragonSlayer}) can alter its behavior by changing its strategy.
|
||||
*
|
||||
@ -17,6 +21,7 @@ public class App {
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
// GoF Strategy pattern
|
||||
System.out.println("Green dragon spotted ahead!");
|
||||
DragonSlayer dragonSlayer = new DragonSlayer(new MeleeStrategy());
|
||||
dragonSlayer.goToBattle();
|
||||
@ -26,5 +31,19 @@ public class App {
|
||||
System.out.println("Black dragon lands before you.");
|
||||
dragonSlayer.changeStrategy(new SpellStrategy());
|
||||
dragonSlayer.goToBattle();
|
||||
|
||||
// Java 8 Strategy pattern
|
||||
System.out.println("Green dragon spotted ahead!");
|
||||
dragonSlayer = new DragonSlayer(
|
||||
() -> System.out.println("With your Excalibur you severe the dragon's head!"));
|
||||
dragonSlayer.goToBattle();
|
||||
System.out.println("Red dragon emerges.");
|
||||
dragonSlayer.changeStrategy(() -> System.out.println(
|
||||
"You shoot the dragon with the magical crossbow and it falls dead on the ground!"));
|
||||
dragonSlayer.goToBattle();
|
||||
System.out.println("Black dragon lands before you.");
|
||||
dragonSlayer.changeStrategy(() -> System.out.println(
|
||||
"You cast the spell of disintegration and the dragon vaporizes in a pile of dust!"));
|
||||
dragonSlayer.goToBattle();
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ package com.iluwatar.strategy;
|
||||
* Strategy interface.
|
||||
*
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface DragonSlayingStrategy {
|
||||
|
||||
void execute();
|
||||
|
Loading…
x
Reference in New Issue
Block a user