Strategy Pattern
The Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. This pattern allows a client to choose which algorithm to use at runtime.
When to Use:
Use the Strategy pattern when you have multiple algorithms for a specific task and you want to choose the appropriate algorithm at runtime.
Example
Pros:
- Promotes Open/Closed Principle.
- Easy to switch between different algorithms.
Cons:
- Increases the number of classes in the application.
- Clients must be aware of different strategies.