未验证 提交 18a1a725 编写于 作者: I ignite1771 提交者: GitHub

#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: NSubhrodip Mohanta <subhrodipmohanta@gmail.com>
上级 25ed2540
......@@ -122,6 +122,51 @@ Program output:
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
![alt text](./etc/strategy_urm.png "Strategy")
......
......@@ -14,6 +14,19 @@ package com.iluwatar.strategy {
interface DragonSlayingStrategy {
+ 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 {
- LOGGER : Logger {static}
+ MeleeStrategy()
......@@ -30,7 +43,10 @@ package com.iluwatar.strategy {
+ execute()
}
}
Strategy ..+ LambdaStrategy
Strategy --> "-dragonSlayingStrategy" DragonSlayingStrategy
DragonSlayer --> "-strategy" DragonSlayingStrategy
Strategy ..|> DragonSlayingStrategy
MeleeStrategy ..|> DragonSlayingStrategy
ProjectileStrategy ..|> DragonSlayingStrategy
SpellStrategy ..|> DragonSlayingStrategy
......
strategy/etc/strategy_urm.png

28.6 KB | W: | H:

strategy/etc/strategy_urm.png

140.0 KB | W: | H:

strategy/etc/strategy_urm.png
strategy/etc/strategy_urm.png
strategy/etc/strategy_urm.png
strategy/etc/strategy_urm.png
  • 2-up
  • Swipe
  • Onion skin
......@@ -45,7 +45,7 @@ public class App {
/**
* Program entry point.
*
*
* @param args command line args
*/
public static void main(String[] args) {
......@@ -60,7 +60,7 @@ public class App {
dragonSlayer.changeStrategy(new SpellStrategy());
dragonSlayer.goToBattle();
// Java 8 Strategy pattern
// Java 8 functional implementation Strategy pattern
LOGGER.info("Green dragon spotted ahead!");
dragonSlayer = new DragonSlayer(
() -> LOGGER.info("With your Excalibur you severe the dragon's head!"));
......@@ -73,5 +73,16 @@ public class App {
dragonSlayer.changeStrategy(() -> LOGGER.info(
"You cast the spell of disintegration and the dragon vaporizes in a pile of dust!"));
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();
}
}
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();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册