Monday, 15 April 2013

Strategy Pattern V/S Decorator Pattern -



Strategy Pattern V/S Decorator Pattern -

i learning design patterns.

i came across 2 patterns.

1) strategy pattern

2) decorator pattern

according implementation found both of them confusing.

what understood is:-

strategy pattern :-

strategy pattern gives several algorithms can used perform particular operation or task.

decorator pattern :-

decorator pattern adds functionality component.

in fact found strategy pattern , decorator pattern can used interchangeably.

here link :- when , how strategy pattern can applied instead of decorator pattern?

so having confusion difference between strategy pattern , decorator pattern?

please body explain difference between both same example.

and please tell me when strategy pattern should used , when decorator pattern should used?

the strategy pattern allows change implementation of used @ runtime.

the decorator pattern allows augment (or add together to) existing functionality additional functionality @ run time.

the key difference in change vs augment

in 1 of questions linked points out strategy pattern consumer aware different options exist, whereas decorator pattern consumer not aware of additional functionality.

as example, imagine writing sort collection of elements. write interface isortingstrategy can implement several different sorting strategies bubblesortstrategy, quicksortstrategy, radixsortstrategy, application, based on criteria of existing list chooses appropriate strategy utilize sort list. illustration if list has fewer 10 items utilize radixsortstrategy, if fewer 10 items have been added list since lastly sort utilize bubblesortstrategy otherwise utilize quicksortstrategy.

we changing type of sort @ runtime (to more efficient based on information.) strategy pattern.

now imagine asks provide log of how each sorting algorithm used actual sort , restrict sorting admin users. can add together both of these pieces of functionality creating decorator enhancers any isortingstrategy. create decorator logs used sort , type of decorated sorting strategy. , add together decorator checked if current user administrator before called decorated sorting strategy.

here adding new functionality of any sorting strategy using decorator, not swapping out core sorting functionality (we used different strategies alter that)

here illustration of how decorators might look:

public interface isortingstrategy { void sort(ilist<int> listtosort); } public class loggingdecorator { private isortingstrategy decorated; public loggingdecorator(isortingstrategy decorated) { this.decorated=decorated; } void sort(ilist<int> listtosort) { log("sorting using strategy: " + decorated.tostring(); decorated.sort(listtosort); } } public class authorisingdecorator { 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 allowed sort"); } } }

design-patterns strategy-pattern

No comments:

Post a Comment