The Strategy Design Pattern is a behavioral design pattern that allows you to define a family of algorithms, encapsulate each one of them, and make them interchangeable.
The Strategy Design Pattern is like having a toolkit of driving techniques for your car. It's a fundamental concept in software design that allows us to organize and manage various driving strategies, making them easy to switch and use.
Let's explain the definition of Strategy pattern:
The Family of Algorithms
Imagine you have a Car
, and you want it to have different driving strategies. These strategies could include NormalDrive
, SportDrive
, EcoDrive
,and more. Each of these strategies represents a unique way of driving. Think of these as the "family of algorithms" that the Strategy Design Pattern allows us to work with.
Encapsulation: Wrapping up Strategies
Each driving strategy is encapsulated in its own class. This encapsulation means that the details of how each strategy works are hidden inside these classes. This separation keeps the code neat and makes it easy to add new strategies or change existing ones without affecting the rest of the car. Therefore, Single Responsibility Principle (SRP) - each class should have one specific job and reason to change.
For example, if you want to create a new driving strategy, you would create a new class that follows the same pattern, encapsulating the unique driving logic within it. This new class would implement a common interface, like IDriveBehavior
, which includes a drive
method.
Here's an example of how this might look in code:
Example in Python
- Define an interface or abstract class called
DriveStrategy
:
from abc import ABC, abstractmethod
class DriveStrategy(ABC):
@abstractmethod
def drive(self):
pass
- Implement concrete classes that represent different driving strategies:
class NormalDrive(DriveStrategy):
def drive(self):
return "Driving normally"
class SportDrive(DriveStrategy):
def drive(self):
return "Driving in a sporty manner"
class EcoDrive(DriveStrategy):
def drive(self):
return "Driving in an eco-friendly manner"
- In the
Car
class, add a reference to theDriveStrategy
interface:
class Car:
def __init__(self, drive_strategy):
self.drive_strategy = drive_strategy
def set_drive_strategy(self, drive_strategy):
self.drive_strategy = drive_strategy
def drive(self):
if self.drive_strategy is not None:
self.drive_strategy.drive()
- Now you can create a
Car
object and dynamically set its driving strategy:
# Client Class
if __name__ == '__main__':
car = Car(NormalDrive())
car.drive()
car.set_drive_strategy(SportDrive())
car.drive()
car.set_drive_strategy(EcoDrive())
car.drive()
Let’s have the code example in Java:
public interface IDriveBehavior {
void drive();
}
public class NormalDrive implements IDriveBehavior {
@Override
public void drive() {
// Implement normal driving logic here
}
}
public class SportDrive implements IDriveBehavior {
@Override
public void drive() {
// Implement sporty driving logic here
}
}
public class EcoDrive implements IDriveBehavior {
@Override
public void drive() {
// Implement eco-friendly driving logic here
}
}
Each driving strategy class encapsulates its unique way of driving, and they all adhere to the same interface. This ensures that you can easily switch between driving strategies without causing chaos in your car's code.
In summary, the Strategy Design Pattern is like having a collection of tools for your car, where each tool represents a different driving strategy. You can swap these tools in and out as needed, and your car will smoothly adapt to different driving styles. It's a powerful concept in software design that promotes flexibility and maintainability in your code.