The strategy Pattern
I’m still pretty deep into java tutorials and a little bit confused. So I decided to take a break and do some research on my presentation this week. The Strategy design pattern.
What is the Strategy Design Pattern?
The Strategy Design Pattern is a behavioral design pattern that enables selecting an algorithm’s behavior at runtime. It involves defining a family of algorithms, encapsulating each one, and making them interchangeable. This pattern allows algorithms to vary independently of the clients that use them.
What are the advantages?
- Flexibility: You can easily switch between different algorithms at runtime.
- Maintainability Adding new techniques doesn’t require changes to existing code.
- Encapsulation Each cooking technique is encapsulated in its own class or record.
Disadvantages
- Increased Number of Classes/Records: Requires creating a separate class or record for each cooking technique.
- Complexity: Can increase the overall complexity of the codebase due to the additional abstraction layer.
Relation to SOLID
- Single Responsibility Principle: Each technique class/record has a single responsibility.
- Open/Closed Principle: The system can be extended with new techniques without modifying existing code.
- Liskov Substitution Principle: Techniques can be used interchangeably as they implement the same interface.
- Interface Segregation Principle: Techniques focus on specific interfaces.
- Dependency Inversion Principle: High-level modules depend on abstractions (Technique interface), not on concrete implementations.