Skip to Content.
Sympa Menu

gang-of-4-patterns - RE: [gang-of-4-patterns] Pattern question: state vs. strategy

gang-of-4-patterns AT lists.cs.illinois.edu

Subject: Design Patterns discussion

List archive

RE: [gang-of-4-patterns] Pattern question: state vs. strategy


Chronological Thread 
  • From: Ralph Johnson <johnson AT cs.uiuc.edu>
  • To: gang-of-4-patterns AT cs.uiuc.edu, jfreeman AT midwaygames.com, kenchoweb AT hotmail.com
  • Cc:
  • Subject: RE: [gang-of-4-patterns] Pattern question: state vs. strategy
  • Date: Wed, 6 Jul 2005 17:49:47 -0500 (CDT)
  • List-archive: <http://mail.cs.uiuc.edu/pipermail/gang-of-4-patterns>
  • List-id: Design Patterns discussion <gang-of-4-patterns.cs.uiuc.edu>

The class diagrams are pretty similar, but the code usually
looks a lot different. The State objects usually have no
instance variables and lots of methods that are not particuarlly
related to each other. There is very low coupling (or low cohesion)
beteween the methods of the state objects. The strategy objects
often (but not always) have instance variables and often have a
lot of methods in them that are called by other methods. Usually
there are a few public methods that call a bunch of private methods.
The state classes cause the context object to switch state, while
the strategy classes never tell their context to switch to a different
strategy.

The purpose of the patterns is fairly different. You think of a
state pattern as a way to implement an object whose behavior is
state dependent. You think of a strategy pattern as a way to
encapsulate algorithms and switch from one to another. Of course,
if the only part of the object that is state dependent is one
method then there isn't much difference.


-Ralph





Archive powered by MHonArc 2.6.16.

Top of Page