File tree Expand file tree Collapse file tree 8 files changed +96
-0
lines changed
src/main/java/com/iluwatar Expand file tree Collapse file tree 8 files changed +96
-0
lines changed Original file line number Diff line number Diff line change 3838 <module >memento</module >
3939 <module >observer</module >
4040 <module >state</module >
41+ <module >strategy</module >
4142 </modules >
4243
4344<build >
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public interface DragonSlayingStrategy {
4+
5+ void execute ();
6+
7+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments