Strategy Design Pattern

Hi, Today we’ll be going a step forward in the design of software architecture. In this article, I’ll be going through the strategy pattern.
Why do we need this? If we use this pattern in our coding style, our code will be more flexible and later if you want to alter or extend the code that varies, you can do without affecting that does not.
How?
Very important principle #1 in design ->
take what varies and encapsulate it … and it will not affect the rest of our code
code that changes ← — — -> code that stays the same
Very important principle #2 in design ->
program to an interface/supertype not an implementation !!!
Abstract superclass would be perfect too …
— the actual runtime object is not locked into the code
— the type of variable should be a supertype/interface: can be of any
concrete implementationDog dog = new Dog(); not so good “programming to an implementation”
Animal dog = new Dog(); GOOD “programming to an interface”Sometimes it is good to separate behaviors from implementation: easier to reuse it,we can add new behavior without modifying any of our existing behavior classes !!!
SOLID PRINCIPLE !!!
Very important principle #3 in design ->
Favor composition over inheritance !!!
Composition: HAS-A relationship -> it gives you a lot more flexibility
1. you can encapsulate pieces of stuff into their own set of classes
2. YOU CAN CHANGE BEHAVIOR AT RUNTIME with interfaces
So composition is used in several design patterns
Inheritance: IS-A relationship
Now let's start with an example of Strategy Design:
In this example, we will be creating a very basic calculator using the Strategy Pattern.
We are creating Strategy which will be the base Interface of our basic operations like Add, Multiply.
public interface Strategy {
public void operation(int num1, int num2);
}
Now let’s create Add and multiply classes: Add and Multiply will be implementing Strategy interface so that later if I want to create a new Strategy called Subtract, I can do that very easily by just implementing the same interface.
//Add.javapublic class Add implements Strategy{@Override
public void operation(int num1, int num2) {
System.out.println(num1+num2);
}
}//Multiply.javapublic class Multiply implements Strategy{@Override
public void operation(int num1, int num2) {
System.out.println(num1*num2);
}
}
Now we’ll create a Manager class that will run the strategy based on the object passed.
//Manager.java
public class Manager implements Strategy{private Strategy strategy;
public void setStrategy(Strategy strategy){
this.strategy = strategy;
}@Override
public void operation(int num1, int num2) {
this.strategy.operation(num1, num2);
}
}
Now let’s see how I will call my strategy by just changing the object
public class MainClass{public static void main(String[] args) {
Manager manager = new Manager();
manager.setStrategy(new Multiply());
manager.operation(5, 5);// this will result 25
manager.setStrategy(new Add());
manager.operation(5, 5); // now the result will be 10. }
}
Now If I want to introduce a new strategy which is Subtract. I can do that by simply creating a new class like this
public class Subtract implements Strategy{@Override
public void operation(int num1, int num2) {
System.out.println(num1-num2);
}
}
And I can call it something like this:
manager.setStrategy(new Subtract());
manager.operation(10,5);//result 5
This is how we can increase our code’s readability as well as expandability using Strategy pattern.
Stay tuned, next is the Null Design Pattern. 🙂