Strategy Design Pattern using a Java Sample

Introduction 

A strategic design pattern is a behavioural design pattern in which the behaviour of the object is encapsulated with a common function name.

Where to use Strategic design pattern?

When different customer have a common strategy, (common operation name), but a different action to perform; then we can use a strategic design pattern.

Why to use Strategic design pattern?

As we discussed previously customers have common operation name to perform, so that we can share a common interface to all customers to perform their actions as per there need.
 
Players in this pattern
  • Strategy: It defines contract with common operation.
  • ConcreteStrategy : It implements contact defined by Strategy.
  • Product: It fulfil customer requirements using ConcreteStrategy objects by referring Strategy contract. 
We will see strategic design pattern with an example:

Problem definition

Define a common operation for a student to access different actions, such as addition, subtraction, multiplication, and division.
 
Players in this case:
  • Strategy : IOperator
  • ConcreteStrategy: AddOperator, SubtractOperator, MultiplyOperator, DivisionOperator.
  • Product : Calculator
Below is the IOperator contract which defined a common operation called ExecuteOperation
  1. package com.sdp;  
  2.   
  3. public interface IOperator {  
  4.     void executeOperation(int a, int b);  
  5. }  
Below are the concrete operators which implement a contract defined by IOperator:
  1. package com.sdp;  
  2.   
  3. public class AddOperator implements IOperator {  
  4.     @Override  
  5.     public void executeOperation(int a, int b) {  
  6.         System.out.println("AddOperator result: " + (a + b));  
  7.     }  
  8. }  
  1. package com.sdp;  
  2.   
  3. public class SubtractOperator implements IOperator {  
  4.     @Override  
  5.     public void executeOperation(int a, int b) {  
  6.         System.out.println("SubtractOperator result: " + (a - b));  
  7.     }  
  8. }  
  1. package com.sdp;  
  2.   
  3. public class MultiplyOperator implements IOperator {  
  4.     @Override  
  5.     public void executeOperation(int a, int b) {  
  6.         System.out.println("MultiplyOperator result: " + (a * b));  
  7.     }  
  8. }  
  1. package com.sdp;  
  2.   
  3. public class DivisionOperator implements IOperator {  
  4.     @Override  
  5.     public void executeOperation(int a, int b) {  
  6.         System.out.println("DivisionOperator result: " + (a / b));  
  7.     }  
  8. }  
Below is the our product calculator which is going to be used by customers with the help of the IOperator contract:
  1. package com.sdp;  
  2.   
  3. public class Calculator {  
  4.   
  5.     private IOperator _operator;  
  6.   
  7.     public Calculator(IOperator ioperator)  
  8.     {  
  9.         _operator = ioperator;  
  10.     }  
  11.   
  12.     public void Calculate(int a, int b)  
  13.     {  
  14.         _operator.executeOperation(a, b);  
  15.     }  
  16. }  
Below is the Student class where the student is using the Calculator to fulfill their requirement: 
  1. package com.sdp;  
  2.   
  3. public class Student {  
  4.   
  5.     public static void main(String[] args) {  
  6.         Calculator oprt = new Calculator(new AddOperator());  
  7.         oprt.Calculate(2010);  
  8.   
  9.         oprt = new Calculator(new SubtractOperator());  
  10.         oprt.Calculate(2010);  
  11.   
  12.         oprt = new Calculator(new MultiplyOperator());  
  13.         oprt.Calculate(2010);  
  14.   
  15.         oprt = new Calculator(new DivisionOperator());  
  16.         oprt.Calculate(2010);  
  17.     }  
  18. }  
Below is the snap of output window of above code:
 
 

Summary

In this article, we learned what a strategic design pattern is and how to use it.


Similar Articles