oop - Difference between State pattern and and Strategy pattern -
looking @ gof patterns find similarities between state , stategy pattern rather striking. both swap out polymorphic classes modify behavior. else found same?
what exact differences?
the state , strategy patterns similar in sense both of them encapsulate behavior in separate objects , use composition delegate composed object implement behavior , both of them provide flexibility change behavior dynamically changing composed object @ run-time. there key differences :
in state pattern, client knows nothing state objects. state changes happen transparently client. client calls methods on context, context oversees own state. because client not aware of state changes, appears client though context instantiated different class each time there change in behavior due state change. object appear change class official definition of pattern states. pattern built around well-defined series of state transitions. changing state key pattern's existence.
even though strategy pattern provides flexibility change behavior changing composed strategy object dynamically, there appropriate strategy object set each context. ie though pattern provides way change composed strategy object dynamically, there won't of need it. if has done, client change. client call setter method on context , pass new strategy object. behavior changes not transparent client , initiated , controlled client. pattern not encourage series of well-defined behavior changes state pattern. client knows strategy objects , set appropriate strategy object in context while creating it. client controls strategy object context uses, in state pattern, client knows nothing state object(s) context uses
for additional information pls refer below link http://myrandomsparks.blogspot.in/2012/05/strategy-vs-state-pattern.html
Comments
Post a Comment