Skip to content

Commit 18a1a72

Browse files
ignite1771ohbus
andauthored
iluwatar#1469 Add lambda expressions implementation for Strategy Pattern (iluwatar#1631)
* iluwatar#1469 Add Strategy Pattern lambda implementation After Java 8. Also take advantage of enum * iluwatar#1469 Update class diagrams * iluwatar#1469 Update README.md Add lambda programmatic example * iluwatar#1469 Remove unused imports Co-authored-by: Subhrodip Mohanta <[email protected]>
1 parent 25ed254 commit 18a1a72

File tree

5 files changed

+103
-2
lines changed

5 files changed

+103
-2
lines changed

strategy/README.md

+45
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,51 @@ Program output:
122122
You cast the spell of disintegration and the dragon vaporizes in a pile of dust!
123123
```
124124

125+
What's more, the new feature Lambda Expressions in Java 8 provides another approach for the implementation:
126+
127+
```java
128+
public class LambdaStrategy {
129+
130+
private static final Logger LOGGER = LoggerFactory.getLogger(LambdaStrategy.class);
131+
132+
public enum Strategy implements DragonSlayingStrategy {
133+
MeleeStrategy(() -> LOGGER.info(
134+
"With your Excalibur you severe the dragon's head!")),
135+
ProjectileStrategy(() -> LOGGER.info(
136+
"You shoot the dragon with the magical crossbow and it falls dead on the ground!")),
137+
SpellStrategy(() -> LOGGER.info(
138+
"You cast the spell of disintegration and the dragon vaporizes in a pile of dust!"));
139+
140+
private final DragonSlayingStrategy dragonSlayingStrategy;
141+
142+
Strategy(DragonSlayingStrategy dragonSlayingStrategy) {
143+
this.dragonSlayingStrategy = dragonSlayingStrategy;
144+
}
145+
146+
@Override
147+
public void execute() {
148+
dragonSlayingStrategy.execute();
149+
}
150+
}
151+
}
152+
```
153+
154+
And here's the dragonslayer in action.
155+
156+
```java
157+
LOGGER.info("Green dragon spotted ahead!");
158+
dragonSlayer.changeStrategy(LambdaStrategy.Strategy.MeleeStrategy);
159+
dragonSlayer.goToBattle();
160+
LOGGER.info("Red dragon emerges.");
161+
dragonSlayer.changeStrategy(LambdaStrategy.Strategy.ProjectileStrategy);
162+
dragonSlayer.goToBattle();
163+
LOGGER.info("Black dragon lands before you.");
164+
dragonSlayer.changeStrategy(LambdaStrategy.Strategy.SpellStrategy);
165+
dragonSlayer.goToBattle();
166+
```
167+
168+
Program output is the same as above one.
169+
125170
## Class diagram
126171

127172
![alt text](./etc/strategy_urm.png "Strategy")

strategy/etc/strategy.urm.puml

+16
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ package com.iluwatar.strategy {
1414
interface DragonSlayingStrategy {
1515
+ execute() {abstract}
1616
}
17+
class LambdaStrategy {
18+
- LOGGER : Logger {static}
19+
+ LambdaStrategy()
20+
}
21+
enum Strategy {
22+
+ MeleeStrategy {static}
23+
+ ProjectileStrategy {static}
24+
+ SpellStrategy {static}
25+
- dragonSlayingStrategy : DragonSlayingStrategy
26+
+ execute()
27+
+ valueOf(name : String) : Strategy {static}
28+
+ values() : Strategy[] {static}
29+
}
1730
class MeleeStrategy {
1831
- LOGGER : Logger {static}
1932
+ MeleeStrategy()
@@ -30,7 +43,10 @@ package com.iluwatar.strategy {
3043
+ execute()
3144
}
3245
}
46+
Strategy ..+ LambdaStrategy
47+
Strategy --> "-dragonSlayingStrategy" DragonSlayingStrategy
3348
DragonSlayer --> "-strategy" DragonSlayingStrategy
49+
Strategy ..|> DragonSlayingStrategy
3450
MeleeStrategy ..|> DragonSlayingStrategy
3551
ProjectileStrategy ..|> DragonSlayingStrategy
3652
SpellStrategy ..|> DragonSlayingStrategy

strategy/etc/strategy_urm.png

111 KB
Loading

strategy/src/main/java/com/iluwatar/strategy/App.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class App {
4545

4646
/**
4747
* Program entry point.
48-
*
48+
*
4949
* @param args command line args
5050
*/
5151
public static void main(String[] args) {
@@ -60,7 +60,7 @@ public static void main(String[] args) {
6060
dragonSlayer.changeStrategy(new SpellStrategy());
6161
dragonSlayer.goToBattle();
6262

63-
// Java 8 Strategy pattern
63+
// Java 8 functional implementation Strategy pattern
6464
LOGGER.info("Green dragon spotted ahead!");
6565
dragonSlayer = new DragonSlayer(
6666
() -> LOGGER.info("With your Excalibur you severe the dragon's head!"));
@@ -73,5 +73,16 @@ public static void main(String[] args) {
7373
dragonSlayer.changeStrategy(() -> LOGGER.info(
7474
"You cast the spell of disintegration and the dragon vaporizes in a pile of dust!"));
7575
dragonSlayer.goToBattle();
76+
77+
// Java 8 lambda implementation with enum Strategy pattern
78+
LOGGER.info("Green dragon spotted ahead!");
79+
dragonSlayer.changeStrategy(LambdaStrategy.Strategy.MeleeStrategy);
80+
dragonSlayer.goToBattle();
81+
LOGGER.info("Red dragon emerges.");
82+
dragonSlayer.changeStrategy(LambdaStrategy.Strategy.ProjectileStrategy);
83+
dragonSlayer.goToBattle();
84+
LOGGER.info("Black dragon lands before you.");
85+
dragonSlayer.changeStrategy(LambdaStrategy.Strategy.SpellStrategy);
86+
dragonSlayer.goToBattle();
7687
}
7788
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.iluwatar.strategy;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class LambdaStrategy {
7+
8+
private static final Logger LOGGER = LoggerFactory.getLogger(LambdaStrategy.class);
9+
10+
public enum Strategy implements DragonSlayingStrategy {
11+
MeleeStrategy(() -> LOGGER.info(
12+
"With your Excalibur you severe the dragon's head!")),
13+
ProjectileStrategy(() -> LOGGER.info(
14+
"You shoot the dragon with the magical crossbow and it falls dead on the ground!")),
15+
SpellStrategy(() -> LOGGER.info(
16+
"You cast the spell of disintegration and the dragon vaporizes in a pile of dust!"));
17+
18+
private final DragonSlayingStrategy dragonSlayingStrategy;
19+
20+
Strategy(DragonSlayingStrategy dragonSlayingStrategy) {
21+
this.dragonSlayingStrategy = dragonSlayingStrategy;
22+
}
23+
24+
@Override
25+
public void execute() {
26+
dragonSlayingStrategy.execute();
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)