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
- package com.sdp;
-
- public interface IOperator {
- void executeOperation(int a, int b);
- }
Below are the concrete operators which implement a contract defined by IOperator:
- package com.sdp;
-
- public class AddOperator implements IOperator {
- @Override
- public void executeOperation(int a, int b) {
- System.out.println("AddOperator result: " + (a + b));
- }
- }
- package com.sdp;
-
- public class SubtractOperator implements IOperator {
- @Override
- public void executeOperation(int a, int b) {
- System.out.println("SubtractOperator result: " + (a - b));
- }
- }
- package com.sdp;
-
- public class MultiplyOperator implements IOperator {
- @Override
- public void executeOperation(int a, int b) {
- System.out.println("MultiplyOperator result: " + (a * b));
- }
- }
- package com.sdp;
-
- public class DivisionOperator implements IOperator {
- @Override
- public void executeOperation(int a, int b) {
- System.out.println("DivisionOperator result: " + (a / b));
- }
- }
Below is the our product calculator which is going to be used by customers with the help of the IOperator contract:
- package com.sdp;
-
- public class Calculator {
-
- private IOperator _operator;
-
- public Calculator(IOperator ioperator)
- {
- _operator = ioperator;
- }
-
- public void Calculate(int a, int b)
- {
- _operator.executeOperation(a, b);
- }
- }
Below is the Student class where the student is using the Calculator to fulfill their requirement:
- package com.sdp;
-
- public class Student {
-
- public static void main(String[] args) {
- Calculator oprt = new Calculator(new AddOperator());
- oprt.Calculate(20, 10);
-
- oprt = new Calculator(new SubtractOperator());
- oprt.Calculate(20, 10);
-
- oprt = new Calculator(new MultiplyOperator());
- oprt.Calculate(20, 10);
-
- oprt = new Calculator(new DivisionOperator());
- oprt.Calculate(20, 10);
- }
- }
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.