Strategy Pattern V/S Decorator Pattern -


I'm learning some design patterns.

I have just come in two samples.

  1. Strategy Pattern

  2. Decorator Pattern

According to the implementation I What some of them found is misleading.

What do I understand: -

Strategy Pattern: -

Strategy Pattern Returns Many Algorithms That Can Be Used for Special Operations Or work

Decorator Pattern: -

The decorator adds some functionality to the pattern component.

In fact I have found that the strategy pattern and decorator patterns can also be used alternately.

Here's the link: -

So I am confused: What is the difference between strategy patterns and decorator patterns?

When the strategy pattern should be used and when the decorator pattern should be used?

The difference between the two with the same example of exp.

The implementation of the strategy pattern you use on the change runtime

Decorator Pattern allows you to increase (or add) existing functionality in additional functionality on runtime.

The main difference is change rise

You also tell one of the questions related to them that consumers know the strategy pattern Different options exist, while the decorator pattern with the consumer will not be aware of additional functionality.

As an example, imagine that you are writing to sort a collection of elements that have to write an interface ISortingStrategy have so many different pruning strategies < to code> BubbleSortStrategy , QuickSortStrategy , RadixSortStrategy , then used to sort the list based on certain criteria for its application, the current list Chooses the most appropriate strategy for. For example, if the list is less then 10 before we RadixSortStrategy will use, if fewer than 10 items have been added to the list since the last we will use BubbleSortStrategy Otherwise we will use QuickSortStrategy .

We are changing the type of type on runtime (to be more efficient based on some additional information.) This is the pattern of strategy.

Think now that someone asks us to log in, how often each sorting algorithm is used to sort a real and the admin user is used to restrict sittings. We can add the functionality of these two things to create a decorator who Growing any ISortingStrategy we can make a decorator who logs that some sorting is The sort of sorting strategy was used to sort

Here we are adding new functionality of any sorting strategy using decorator and we can add another decorator Which are checked TI is the current user is an administrator. But we are not swapping core sorting functionality (we have used different strategies to change it)

Here's an example of looking decorative:

  Public Interface I.Setting Strategy (Zero Sort (IELIT and Lieutenius; Int & gt; Catalog Lector);} Public Class Logging Detector: Istingstroxy {Personal Editing Strawley Decorated; Public A Logingdicoretr (decorated were Itotingstroji) {this.decorated = decorated;} void Sort (IList & LT; integer & gt; listToSort) using {log ( "strategy sorting:" + decorated.ToString (); decorated.Sort (listToSort );}} public class AuthorisingDecorator: ISortingStrategy {private ISortingStrategy decorated public AuthorisingDecorator (decorated ISortingStrategy) {this.decorated = decorated;} void Sort (IList & LT; integer & gt; listToSort) {if (CurrentUserIsAdministrator ()) {decorated .Sort (listToSort);} else {New UserNotAuthorizedException Throw ("Only Administrators are allowed to sort");}}}  

Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -