I just came across two patterns.
Strategy Pattern
Decorator
Strategy Pattern :-
Strategy pattern gives several algorithms that can be used to perform particular operation or task.
Decorator Pattern :-
Decorator pattern adds some functionality to component.
In fact I have found that Strategy Pattern and Decorator Pattern can also be used interchangeably.
Here is the link :- When and How Strategy pattern can be applied instead of decorator pattern?
What is the difference between Strategy Pattern and Decorator Pattern?
when Strategy Pattern should be used and when Decorator Pattern should be used?
Explain the difference between both with the same example.
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");
}
}
}