Strategy Pattern V/S Decorator Pattern
Strategy_pattern
- Defines a family of algorithms,
- Encapsulates each algorithm, and
- Makes the algorithms interchangeable within that family.
Use Strategy pattern when you have to change algorithm dynamically at run time.
Decorator
Decorator pattern dynamically changes the functionality of an object at runtime without impacting the existing functionality of the objects.
When to use:
- Add additional functionalities/responsibilities dynamically
- Remove functionalities/responsibilities dynamically
- Avoid too much of sub-classing to add additional responsibilities.
Drawbacks:
- Overuse of Open Closed principle ( Open for extension and Closed for modification). Use this feature sparingly where the code is least likely changed.
- Too many small classes and will add maintenance overhead.
Key difference:
Strategy lets you change the guts of an object. Decorator lets you change the skin.
Few more useful posts:
When to Use the Decorator Pattern?
Real World Example of the Strategy Pattern
strategy by sourcemaking
The strategy pattern allows you to change the implementation of something used at runtime.
The decorator pattern allows you augment (or add to) existing functionality with additional functionality at run time.
The key difference is in the change vs augment
In one of the questions you linked to it also points out that with the strategy pattern the consumer is aware that the different options exist, whereas with the decorator pattern the consumer would not be aware of the additional functionality.
As an example, imagine you are writing something to sort a collection of elements. So you write an interface ISortingStrategy
you can then implement several different sorting strategies BubbleSortStrategy
, QuickSortStrategy
, RadixSortStrategy
, then your application, based on some criteria of the existing list chooses the most appropriate strategy to use to sort the list. So for example if the list has fewer than 10 items we will use RadixSortStrategy
, if fewer than 10 items have been added to the list since the last sort we will use BubbleSortStrategy
otherwise we will use QuickSortStrategy
.
We are changing the type of sort at runtime (to be more efficient based on some extra information.) this is the strategy pattern.
Now imagine someone asks us to provide a log of how often each sorting algorithm is used to do an actual sort and to restrict sorting to admin users. We can add both of these pieces of functionality by creating a decorator which enhances any ISortingStrategy
. We could create a decorator which logs that it was used to sort something and the type of the decorated sorting strategy. And we could add another decorator that checks if the current user is an administrator before calling the decorated sorting strategy.
Here we are adding new functionality to any sorting strategy using the decorator, but are not swapping out the core sorting functionality (we used the different strategies to change that)
Here is an example of how the decorators might look:
public interface ISortingStrategy
{
void Sort(IList<int> listToSort);
}
public class LoggingDecorator : ISortingStrategy
{
private ISortingStrategy decorated;
public LoggingDecorator(ISortingStrategy decorated)
{
this.decorated=decorated;
}
void Sort(IList<int> listToSort)
{
Log("sorting using the strategy: " + decorated.ToString();
decorated.Sort(listToSort);
}
}
public class AuthorisingDecorator : ISortingStrategy
{
private ISortingStrategy decorated;
public AuthorisingDecorator(ISortingStrategy decorated)
{
this.decorated=decorated;
}
void Sort(IList<int> listToSort)
{
if (CurrentUserIsAdministrator())
{
decorated.Sort(listToSort);
}
else
{
throw new UserNotAuthorizedException("Only administrators are allowed to sort");
}
}
}