Re: Strategy pattern and "related classes"

From: Robert C. Martin (unclebob_at_objectmentor.com)
Date: 10/30/04


Date: Sat, 30 Oct 2004 08:44:45 -0500

On Mon, 25 Oct 2004 18:02:56 GMT, "H. S. Lahman"
<h.lahman@verizon.net> wrote:

>Responding to Reiersol...
>
>> "Use the Strategy pattern when:
>>
>> * Many related classes differ only in their behavior. Strategies provide
>> a way to configure a class with one of many behaviors."
>>
>> I've been wondering what this means. Differ only in their behavior, as
>> opposed to what?
>
>The pattern might just as easily be called Algorithm. It exists solely
>to substitute the algorithm employed for a particular behavior
>responsibility at run time.

Right. (Another agreement!)

>Contrast that with the State pattern (which might better have been named
>Role). In State one substitutes an entire role consisting of multiple
>behaviors that are consistent with one another.

The difference between Strategy and State is:

1. The state object calls back to the context object to invoke
algorithms that are defined in the context. Strategy actually
contains the algorithm being invoked.

2. There are always several derivatives of State, that represent the
states of the system, and that call back to the context in different
ways. Strategy strongly implies that there will be many different
derivatives, but does not actually require it.

3. The State derivatives know about each other, and swap each other in
and out of the Context in order to change the state of the system.
Strategies typically don't know about each other.

>Note that both patterns represent a form of delegation where the Context
>object has some behavior responsibility (method signature in the
>interface) but the actual behavior implementation of that responsibility
>is substituted at run time.

True, but in strategy the runtime behavior is preformed by the
Strategy derivatives. In State the runtime behavior is *selected* by
the state derivatives but still *performed* by the context.

>One could provide that substitution by
>subclassing Context and then instantiating the relationship between
>Client and the right Context subclass instance.

That's the Template Method pattern.

-----
Robert C. Martin (Uncle Bob) | email: unclebob@objectmentor.com
Object Mentor Inc. | blog: www.butunclebob.com
The Agile Transition Experts | web: www.objectmentor.com
800-338-6716

"The aim of science is not to open the door to infinite wisdom,
 but to set a limit to infinite error."
    -- Bertolt Brecht, Life of Galileo



Relevant Pages

  • Re: Javascript Best Practices Document v1.0
    ... unequivocally assert that a general algorithm must consider every ... (and 100% successful and accurate within its stated context) ... Real web sites are not radically re-designed at the ... > or even allow the end-users to alter the design. ...
    (comp.lang.javascript)
  • Re: Article in Scientific American
    ... I am not aware of such a context. ... Because an algorithm may contain no logical gaps, ... The question of whether a computer verification, ...
    (sci.math)
  • Re: Abstract public member variales?
    ... specific context of processing streams of arbitrary objects. ... However, polymorphic dispatch is ... for the client depending on context. ... the Serializer pattern. ...
    (comp.object)
  • Re: Shooter at Ft Hood survived
    ... I've had personal experience with a similar incident. ... wondering what you missed that, had you been able to know the context, ... this guy start comparing notes that the pattern becomes apparnt. ...
    (sci.military.naval)
  • Re: Shooter at Ft Hood survived
    ... I've had personal experience with a similar incident. ... wondering what you missed that, had you been able to know the context, ... this guy start comparing notes that the pattern becomes apparnt. ...
    (sci.military.naval)