The strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
For instance, a class that performs validation on incoming data may use a strategy pattern to select a validation algorithm based on the type of data, the source of the data, user choice, and/or other discriminating factors. These factors are not known for each case until run-time, and may require radically different validation to be performed. The validation strategies, encapsulated separately from the validating object, may be used by other validating objects in different areas of the system (or even different systems) without code duplication.
Sample code -
public interface Strategy { public void execute(); } public class StrategyOne implements Strategy { @Override public void execute() { System.out.println("This is Strategy One."); } } public class StrategyTwo implements Strategy { @Override public void execute() { System.out.println("This is Strategy Two."); } } public class StrategyThree implements Strategy { @Override public void execute() { System.out.println("This is Strategy Three."); } } public class Context { private Strategy strategy; public Context(Strategy strategy) { this.strategy = strategy; } public void executeStrategy() { strategy.execute(); } }Suppose we have a game with 3 characters and 3 tools, each character can deal with different situations with different tools. We can use Strategy Pattern to implement this design.
As usual, see the uml diagram first -
Code -
public interface Tool { public void useTool(); } public class Stick implements Tool { public void useTool() { System.out.println("Big Stick, Bully is Down."); } } public class iPhone implements Tool { public void useTool() { System.out.println("It's iPhone 5, iOS is updated."); } } public class Key implements Tool { public void useTool() { System.out.println("Correct Key, Door is Opened."); } } public abstract class Character { protected Tool tool; public Character() { } public Character(Tool tool) { this.tool = tool; } public void setTool(Tool tool) { this.tool = tool; } public void useTool() { tool.useTool(); } public abstract void display(); } public class Warrior extends Character { public Warrior() { tool = new Stick(); } public void display(){ System.out.println("This is Warrior."); useTool(); System.out.println(); } } public class Mage extends Character { public Mage() { tool = new iPhone(); } public void display(){ System.out.println("This is Mage."); useTool(); System.out.println(); } } public class Rogue extends Character { public Rogue() { tool = new Key(); } public void display(){ System.out.println("This is Rogue."); useTool(); System.out.println(); } }
Test Code -
public class StrategyGame { public static void main(String[] args) { Warrior w = new Warrior(); Mage m = new Mage(); Rogue r = new Rogue(); w.display(); m.display(); r.display(); w.setTool(new iPhone()); m.setTool(new Key()); r.setTool(new Stick()); w.display(); m.display(); r.display(); } }
Output -
This is Warrior.
Big Stick, Bully is Down.
This is Mage.
It's iPhone 5, iOS is updated.
This is Rogue.
Correct Key, Door is Opened.
This is Warrior.
It's iPhone 5, iOS is updated.
This is Mage.
Correct Key, Door is Opened.
This is Rogue.
Big Stick, Bully is Down.
No comments:
Post a Comment