Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strategy Pattern V/S Decorator Pattern

I just came across two patterns.

  1. Strategy Pattern

  2. 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.

like image 297
Nirav Kamani Avatar asked Oct 17 '14 10:10

Nirav Kamani


People also ask

What is the difference between strategy and decorator pattern?

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.

What is the difference between strategy and template design pattern?

But, the key difference is that Strategy Pattern is about modifying a behaviour of a context in runtime using strategies, while Template Method Pattern is about following a skeleton implementation of an algorithm and modifying its behaviour by overriding methods of the skeleton class in the subclasses.

What is difference between strategy pattern and factory pattern?

The factory pattern is a creational pattern while the strategy is a behavioral pattern which means they were created using different approaches. We can solve problems in real life using both approaches but before using them we can check which approach is simpler and more effective to solve our problem.


1 Answers

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");          }     } } 
like image 83
Sam Holder Avatar answered Sep 21 '22 15:09

Sam Holder