Introduction

Strategy design pattern is a behavioral design pattern in which the behavior of the object is encapsulated with a common function name.

Where to use a Strategic design pattern?

We can use strategic design pattern when a different customer has a common strategy (Common operation name) but a different action to perform.

Why use a Strategic design pattern?

As discussed previously, customers have a common operation name to perform. With a strategic design pattern, we can share a common interface to all customers to perform their actions as per their need.
Players in this pattern
  • Strategy: Defines a contract with a common operation.
  • ConcreteStrategy: Implements contact defined by the Strategy.
  • Product: Fulfills customer requirements using ConcreteStrategy objects by referring Strategy contract.
Now we will see a strategic design pattern with an example:
Problem definition: Define a common operation to student to access different actions like Addition, subtraction, multiplication, division.
Players in this case:
  • Strategy: operator
  • ConcreteStrategy: AddOperator, SubtractOperator, MultiplyOperator, DivisionOperator.
  • Product: Calculator
Below is the operator contract which defines a common operation called ExecuteOperation:
  1. package com.sdp;
  2. public interface IOperator {
  3. void executeOperation(int a, int b);
  4. }
Below are the concrete operators which implement a contract as defined by the operator:
  1. package com.sdp;
  2. public class AddOperator implements IOperator {
  3. @Override
  4. public void executeOperation(int a, int b) {
  5. System.out.println("AddOperator result: " + (a + b));
  6. }
  1. package com.sdp;
  2. public class SubtractOperator implements IOperator {
  3. @Override
  4. public void executeOperation(int a, int b) {
  5. System.out.println("SubtractOperator result: " + (a - b));
  6. }
  7. }
  1. package com.sdp;
  2. public class MultiplyOperator implements IOperator {
  3. @Override
  4. public void executeOperation(int a, int b) {
  5. System.out.println("MultiplyOperator result: " + (a * b));
  6. }
  7. }
  1. package com.sdp;
  2. public class DivisionOperator implements IOperator {
  3. @Override
  4. public void executeOperation(int a, int b) {
  5. System.out.println("DivisionOperator result: " + (a / b));
  6. }
  7. }
Below is the our product calculator which is going to used by customers with the help of operator contract:
  1. package com.sdp;
  2. public class Calculator {
  3. private IOperator _operator;
  4. public Calculator(IOperator ioperator)
  5. {
  6. _operator = ioperator;
  7. }
  8. public void Calculate(int a, int b)
  9. {
  10. _operator.executeOperation(a, b);
  11. }
  12. }
Below is the student class in where the student is using a Calculator to fulfill his/her requirement.
  1. package com.sdp;
  2. public class Student {
  3. public static void main(String[] args) {
  4. Calculator oprt = new Calculator(new AddOperator());
  5. oprt.Calculate(20, 10);
  6. oprt = new Calculator(new SubtractOperator());
  7. oprt.Calculate(20, 10);
  8. oprt = new Calculator(new MultiplyOperator());
  9. oprt.Calculate(20, 10);
  10. oprt = new Calculator(new DivisionOperator());
  11. oprt.Calculate(20, 10);
  12. }
  13. }
Below is the snap of output window of the above code:

Summary

In this blog, we understood about what strategy and design pattern are and how to use them.