What is Strategy Design Pattern?
Strategy design pattern, also called as policy design pattern is utilized for managing (i) algorithms, (ii) relationships and (iii) responsibilities among the objects. Strategy design pattern is a behaviour pattern. In this pattern, a class algorithm or its behaviour can be altered during run time.
Few Strategy design pattern examples include
- Documents being saved in various file formats like .docx, .html, .rtf and excel documents saved as .xls, .csv.
- Files being compressed by .zip, .rar and image files being compressed as .jpeg, .png, .bmp, .tif
- Using various formats like bar chart, points etc, for plotting the same statistics.
Purpose of Strategy Design Pattern
Strategy design pattern is intended to define a group of algorithms, then encapsulate each of the algorithms and permit them to become interchangeable. Strategy aims at letting the algorithm differ independently from Clients who are using it.
Strategy Design Pattern Implementation using Java
Strategy design pattern is now demonstrated with an example. It is illustrated step by step.
Step 1: First a Strategy interface is created which describes an operation.
OperationStrategy.java
1 2 3 |
public interface OperationStrategy { public int arithmeticOperation(int m, int n); } |
Step 2: In the next step, concrete classes are created. Here three implementations are made, one each for addition, subtraction and multiplication.
Addition.java
1 2 3 4 5 6 |
public class Addition implements OperationStrategy{ @Override public int arithmeticOperation(int m, int n) { return m + n; } } |
Subtraction.java
1 2 3 4 5 6 |
public class Subtraction implements OperationStrategy{ @Override public int arithmeticOperation(int m, int n) { return m - n; } } |
Multiplication.java
1 2 3 4 5 6 |
public class Multiplication implements OperationStrategy{ @Override public int arithmeticOperation(int m, int n) { return m * n; } } |
Step 3: The Context Class is created in the next step. The context is used for observing the alteration in its behavior when the strategy is altered.
OperationContext.java
1 2 3 4 5 6 7 8 9 10 |
public class OperationContext { private OperationStrategy strategy; public OperationContext(OperationStrategy strategy){ this.strategy = strategy; } public int performStrategy(int m, int n){ return strategy.arithmeticOperation(m, n); } } |
Step 4: As shown below, StrategyPatternTrial, the demo class, will utilize OperationContext and strategy objects for representing alteration in Context behaviour on the basis of the strategy it uses.
StrategyPatternTrial.java
1 2 3 4 5 6 7 8 9 10 11 |
public class StrategyPatternTrial { public static void main(String[] args) { OperationContext context = new OperationContext(new Addition()); System.out.println("12 + 4 = " + context.performStrategy(12,4)); context = new OperationContext(new Subtraction()); System.out.println("12 - 4 = " + context.performStrategy(12, 4)); context = new OperationContext(new Multiplication()); System.out.println("12 * 4 = " + context.performStrategy(12, 4)); } } |
Step 5: After execution, output of the above program would be:
12 + 4 = 16
12 – 4 = 8
12 * 4 = 48
Leave a Reply