What's the difference between "Chain of responsibility" and "Strategy" patterns? -
i'm raising question because of another question asked here on days ago.
had solve specific problem, , after 2 replies got, realized 2 patterns can solve problem (and other similar).
- chain of responsibility
- strategy
my question is:
what difference between patterns?
they're different.
strategy having generic interface can use provide different implementations of algorithm, or several algorithms or pieces of logic have common dependencies.
for instance, collectionsorter
support sortingstrategy
(merge sort, quick sort, bubble sort). have same interface , purpose, can different things.
in cases may decide determine strategy inside. maybe sorter has heuristics based on collection size etc. of time indeed injected outside. when pattern shines: provides users ability override (or provide) behavior.
this pattern base of now-omnipresent inversion of control. study next once you're done classic patterns.
chain of responsibility having chain of objects go more detailed more generic. each of pieces in chain can provide answer, have different levels of detail.
popular gof example context system. when click on component in desktop app, display? first item in chain for component clicked. next in chain try , display whole containing dialog. next application module... , on.
looks haven't, should, read gof "design patterns" classic.
Comments
Post a Comment