Skip to content

Commit 6157f22

Browse files
committed
added strategy pattern sample
1 parent 2d0905a commit 6157f22

File tree

8 files changed

+96
-0
lines changed

8 files changed

+96
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<module>memento</module>
3939
<module>observer</module>
4040
<module>state</module>
41+
<module>strategy</module>
4142
</modules>
4243

4344
<build>

strategy/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.iluwatar</groupId>
7+
<artifactId>java-design-patterns</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<groupId>com.iluwatar</groupId>
11+
<artifactId>strategy</artifactId>
12+
<version>1.0-SNAPSHOT</version>
13+
<name>strategy</name>
14+
<url>http://maven.apache.org</url>
15+
<dependencies>
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<version>3.8.1</version>
20+
<scope>test</scope>
21+
</dependency>
22+
</dependencies>
23+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.iluwatar;
2+
3+
public class App
4+
{
5+
public static void main( String[] args )
6+
{
7+
System.out.println("Green dragon spotted ahead!");
8+
DragonSlayer dragonSlayer = new DragonSlayer(new MeleeStrategy());
9+
dragonSlayer.goToBattle();
10+
System.out.println("Red dragon emerges.");
11+
dragonSlayer.changeStrategy(new ProjectileStrategy());
12+
dragonSlayer.goToBattle();
13+
System.out.println("Black dragon lands before you.");
14+
dragonSlayer.changeStrategy(new SpellStrategy());
15+
dragonSlayer.goToBattle();
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.iluwatar;
2+
3+
public class DragonSlayer {
4+
5+
private DragonSlayingStrategy strategy;
6+
7+
public DragonSlayer(DragonSlayingStrategy strategy) {
8+
this.strategy = strategy;
9+
}
10+
11+
public void changeStrategy(DragonSlayingStrategy strategy) {
12+
this.strategy = strategy;
13+
}
14+
15+
public void goToBattle() {
16+
strategy.execute();
17+
}
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.iluwatar;
2+
3+
public interface DragonSlayingStrategy {
4+
5+
void execute();
6+
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.iluwatar;
2+
3+
public class MeleeStrategy implements DragonSlayingStrategy {
4+
5+
@Override
6+
public void execute() {
7+
System.out.println("With your Excalibur you severe the dragon's head!");
8+
}
9+
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.iluwatar;
2+
3+
public class ProjectileStrategy implements DragonSlayingStrategy {
4+
5+
@Override
6+
public void execute() {
7+
System.out.println("You shoot the dragon with the magical crossbow and it falls dead on the ground!");
8+
}
9+
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.iluwatar;
2+
3+
public class SpellStrategy implements DragonSlayingStrategy {
4+
5+
@Override
6+
public void execute() {
7+
System.out.println("You cast the spell of disintegration and the dragon vaporizes in a pile of dust!");
8+
}
9+
10+
}

0 commit comments

Comments
 (0)