encapsulation - Using a strategy pattern and a command pattern -


both design patterns encapsulate algorithm , decouple implementation details calling classes. difference can discern strategy pattern takes in parameters execution, while command pattern doesn't.

it seems me command pattern requires information execution available when created, , able delay calling (perhaps part of script).

what determinations guide whether use 1 pattern or other?

i'm including encapsulation hierarchy table of several of gof design patterns explain differences between these 2 patterns. better illustrates each encapsulates explanation makes more sense.

first off, hierarchy lists scope given pattern applicable, or appropriate pattern use encapsulate level of detail, depending on side of table start at.

design pattern encapsulation hierarchy table

as can see table, strategy pattern object hides details of algorithm's implementation, use of different strategy object perform same functionality in different way. each strategy object might optimized particular factor or operate on other parameter; and, through use of common interface, context can safely work either.

the command pattern encapsulates smaller level of detail algorithm. encodes details needed send message object: receiver, selector , arguments. benefit objectifying such tiny part of process execution such messages can invoked along different points of time or location in general way without having hard-code details. allows messages invoked 1 or more times, or passed along different parts of system or multiple systems without requiring details of specific invocation known before execution.

as typical design patterns, not require implementations identical in detail bear pattern name. details can vary in implementation , in data encoded in object versus method arguments.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -