Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Applying the State pattern
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Martin Vuille  
View profile  
 More options Sep 2 2000, 9:31 pm
Newsgroups: comp.object
From: mar...@jpmvrealtime.com (Martin Vuille)
Date: Sun, 03 Sep 2000 01:31:33 GMT
Local: Sat, Sep 2 2000 9:31 pm
Subject: Re: Applying the State pattern

In article <39B10DE2.7C88D...@acm.org>, r...@acm.org wrote:
>According to the state pattern, a class interacts with the state map via
>a Context class. Since your classes differ on in behavior (they have
>different FSMs), you *can* use inheritance and each derived class will
>contain a different FSM context:

>    class BaseClass
>    {
>        // Put common method and data here.
>    }

>    class DerivedClass1 : public BaseClass
>    {
>        FSMContext1 *_fsm;

>        DerivedClass1()
>        : BaseClass()
>        {
>            ...
>            // See "Design Patterns" pp. 305 - 313
>            // re: State pattern.
>            _fsm = new FSMContext1();
>            ...
>        }

>        // If you want to issue a transition, then ...
>        void handleEvent(Event e)
>        {
>           _fsm->transition(this, e);
>        }
>    }

>DerivedClass2 will be similar.

This is not how I had interpreted the GoF's description of the
State pattern. In my mind, DerivedClass1 _is_ the Context Class,
i.e. the class that the rest of the system sees and uses. I
don't see what benefit is provided by the FSMContext1 class
above.

>Regarding your second question, if the FSMs are different, then you will
>need to create two different FSM context classes and all the concrete
>states. I know there is the idea regarding have one FSM inherit and
>extend a base FSM but I that idea does not make sense to me.

It may not make sense in terms of an IS-A relationship, but what I'm
really after is a mechanism for reusing the implementation of a
class that is implemented as an FSMs.

What I have come up with so far is to define a vector
of pointers to the FSM's concrete states in the Context class, so
that the concrete states do not refer to specific concrete state
instances. This allows polymorphism in the concrete states and would
allow me to to reuse the bulk of the concrete state classes and
modify the ones I need to modify.

Each derived Context class' constructor would populate the list
of states with pointers to the appropriate state instances for
that derivative's FSM.

By using the Template pattern on the Context class for the methods
implementing the FSM actions, the actions of the base FSM can be
overriden in derived FSM's when necessary.

I haven't yet figured out how the concrete states would refer
to this list of next state pointers to set the next state.

Only thing is, this is anything but elegant, or easily comprehensible.

MV

Martin Vuille      | Real-time and | Software & Firmware Contracting
JPMV Realtime Inc. |   embedded    | Turnkey Controls Development
Tel.:(613)823-0777 |   control     | System Integration
Fax.:(613)823-3034 |  specialist   | Product Development Consulting


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.