#1469 Add lambda expressions implementation for Strategy Pattern (#1631)

* #1469 Add Strategy Pattern lambda implementation

After Java 8.
Also take advantage of enum

* #1469 Update class diagrams

* #1469 Update README.md

Add lambda programmatic example

* #1469 Remove unused imports

Co-authored-by: Subhrodip Mohanta <subhrodipmohanta@gmail.com>
This commit is contained in:
ignite1771 2021-01-19 14:36:26 +08:00 committed by GitHub
parent 25ed2540f4
commit 18a1a725ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 103 additions and 2 deletions

View File

@ -122,6 +122,51 @@ Program output:
You cast the spell of disintegration and the dragon vaporizes in a pile of dust! You cast the spell of disintegration and the dragon vaporizes in a pile of dust!
``` ```
What's more, the new feature Lambda Expressions in Java 8 provides another approach for the implementation:
```java
public class LambdaStrategy {
private static final Logger LOGGER = LoggerFactory.getLogger(LambdaStrategy.class);
public enum Strategy implements DragonSlayingStrategy {
MeleeStrategy(() -> LOGGER.info(
"With your Excalibur you severe the dragon's head!")),
ProjectileStrategy(() -> LOGGER.info(
"You shoot the dragon with the magical crossbow and it falls dead on the ground!")),
SpellStrategy(() -> LOGGER.info(
"You cast the spell of disintegration and the dragon vaporizes in a pile of dust!"));
private final DragonSlayingStrategy dragonSlayingStrategy;
Strategy(DragonSlayingStrategy dragonSlayingStrategy) {
this.dragonSlayingStrategy = dragonSlayingStrategy;
}
@Override
public void execute() {
dragonSlayingStrategy.execute();
}
}
}
```
And here's the dragonslayer in action.
```java
LOGGER.info("Green dragon spotted ahead!");
dragonSlayer.changeStrategy(LambdaStrategy.Strategy.MeleeStrategy);
dragonSlayer.goToBattle();
LOGGER.info("Red dragon emerges.");
dragonSlayer.changeStrategy(LambdaStrategy.Strategy.ProjectileStrategy);
dragonSlayer.goToBattle();
LOGGER.info("Black dragon lands before you.");
dragonSlayer.changeStrategy(LambdaStrategy.Strategy.SpellStrategy);
dragonSlayer.goToBattle();
```
Program output is the same as above one.
## Class diagram ## Class diagram
![alt text](./etc/strategy_urm.png "Strategy") ![alt text](./etc/strategy_urm.png "Strategy")

View File

@ -14,6 +14,19 @@ package com.iluwatar.strategy {
interface DragonSlayingStrategy { interface DragonSlayingStrategy {
+ execute() {abstract} + execute() {abstract}
} }
class LambdaStrategy {
- LOGGER : Logger {static}
+ LambdaStrategy()
}
enum Strategy {
+ MeleeStrategy {static}
+ ProjectileStrategy {static}
+ SpellStrategy {static}
- dragonSlayingStrategy : DragonSlayingStrategy
+ execute()
+ valueOf(name : String) : Strategy {static}
+ values() : Strategy[] {static}
}
class MeleeStrategy { class MeleeStrategy {
- LOGGER : Logger {static} - LOGGER : Logger {static}
+ MeleeStrategy() + MeleeStrategy()
@ -30,7 +43,10 @@ package com.iluwatar.strategy {
+ execute() + execute()
} }
} }
Strategy ..+ LambdaStrategy
Strategy --> "-dragonSlayingStrategy" DragonSlayingStrategy
DragonSlayer --> "-strategy" DragonSlayingStrategy DragonSlayer --> "-strategy" DragonSlayingStrategy
Strategy ..|> DragonSlayingStrategy
MeleeStrategy ..|> DragonSlayingStrategy MeleeStrategy ..|> DragonSlayingStrategy
ProjectileStrategy ..|> DragonSlayingStrategy ProjectileStrategy ..|> DragonSlayingStrategy
SpellStrategy ..|> DragonSlayingStrategy SpellStrategy ..|> DragonSlayingStrategy

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 140 KiB

View File

@ -45,7 +45,7 @@ public class App {
/** /**
* Program entry point. * Program entry point.
* *
* @param args command line args * @param args command line args
*/ */
public static void main(String[] args) { public static void main(String[] args) {
@ -60,7 +60,7 @@ public class App {
dragonSlayer.changeStrategy(new SpellStrategy()); dragonSlayer.changeStrategy(new SpellStrategy());
dragonSlayer.goToBattle(); dragonSlayer.goToBattle();
// Java 8 Strategy pattern // Java 8 functional implementation Strategy pattern
LOGGER.info("Green dragon spotted ahead!"); LOGGER.info("Green dragon spotted ahead!");
dragonSlayer = new DragonSlayer( dragonSlayer = new DragonSlayer(
() -> LOGGER.info("With your Excalibur you severe the dragon's head!")); () -> LOGGER.info("With your Excalibur you severe the dragon's head!"));
@ -73,5 +73,16 @@ public class App {
dragonSlayer.changeStrategy(() -> LOGGER.info( dragonSlayer.changeStrategy(() -> LOGGER.info(
"You cast the spell of disintegration and the dragon vaporizes in a pile of dust!")); "You cast the spell of disintegration and the dragon vaporizes in a pile of dust!"));
dragonSlayer.goToBattle(); dragonSlayer.goToBattle();
// Java 8 lambda implementation with enum Strategy pattern
LOGGER.info("Green dragon spotted ahead!");
dragonSlayer.changeStrategy(LambdaStrategy.Strategy.MeleeStrategy);
dragonSlayer.goToBattle();
LOGGER.info("Red dragon emerges.");
dragonSlayer.changeStrategy(LambdaStrategy.Strategy.ProjectileStrategy);
dragonSlayer.goToBattle();
LOGGER.info("Black dragon lands before you.");
dragonSlayer.changeStrategy(LambdaStrategy.Strategy.SpellStrategy);
dragonSlayer.goToBattle();
} }
} }

View File

@ -0,0 +1,29 @@
package com.iluwatar.strategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LambdaStrategy {
private static final Logger LOGGER = LoggerFactory.getLogger(LambdaStrategy.class);
public enum Strategy implements DragonSlayingStrategy {
MeleeStrategy(() -> LOGGER.info(
"With your Excalibur you severe the dragon's head!")),
ProjectileStrategy(() -> LOGGER.info(
"You shoot the dragon with the magical crossbow and it falls dead on the ground!")),
SpellStrategy(() -> LOGGER.info(
"You cast the spell of disintegration and the dragon vaporizes in a pile of dust!"));
private final DragonSlayingStrategy dragonSlayingStrategy;
Strategy(DragonSlayingStrategy dragonSlayingStrategy) {
this.dragonSlayingStrategy = dragonSlayingStrategy;
}
@Override
public void execute() {
dragonSlayingStrategy.execute();
}
}
}