🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #460

Re: conversion to state pattern

#460
From: Robert C. Martin
Date: Wed, 23 Jan 2002 19:08
176 lines
5932 bytes
The following is the translation of your CDController to the state
pattern.  


==================cdcontroller.h===============================
#ifndef FSM_CDControllerFSM
#define FSM_CDControllerFSM

#include <stddef.h>

class CDControllerFSM;

//----------------------------------------------
// CDControllerFSMState: The base state class
//----------------------------------------------
class CDControllerFSMState
{
  public: 
    virtual const char* StateName() const = 0;
    virtual void Stop( CDControllerFSM& );
    virtual void Play( CDControllerFSM& );
    virtual void End( CDControllerFSM& );
};

//----------------------------------------------
// State: Idle
//----------------------------------------------
class CDControllerFSMIdleState : public CDControllerFSMState
{
  public: 
    virtual const char* StateName() const
        { return "Idle"; }
    virtual void Play( CDControllerFSM& );
};

//----------------------------------------------
// State: Playing
//----------------------------------------------
class CDControllerFSMPlayingState : public CDControllerFSMState
{
  public: 
    virtual const char* StateName() const
        { return "Playing"; }
    virtual void Stop( CDControllerFSM& );
    virtual void End( CDControllerFSM& );
};

//----------------------------------------------
// CDControllerFSM: The Finite State Machine class
//----------------------------------------------
class CDControllerFSM
{
  public: 
    // Static State variables
    static CDControllerFSMIdleState Idle;
    static CDControllerFSMPlayingState Playing;

    CDControllerFSM(); // default Constructor

    // Event functions
    void Stop() { itsState->Stop( *this ); }
    void Play() { itsState->Play( *this ); }
    void End() { itsState->End( *this ); }

    // State Accessor functions
    void SetState( CDControllerFSMState& theState ) 
    { itsState = &theState; }
    CDControllerFSMState& GetState() const 
    { return *itsState; }

    const char* GetCurrentStateName() const 
    { return itsState->StateName(); }

  private:
    CDControllerFSMState* itsState;
};

#endif



=========================cdcontroller.cpp===================


#include "cdcontroller.h"

CDControllerFSMIdleState CDControllerFSM::Idle;
CDControllerFSMPlayingState CDControllerFSM::Playing;

//----------------------------------------------
// Default Event Functions
//----------------------------------------------

void CDControllerFSMState::Stop(CDControllerFSM& s)
  { s.FSMError("Stop", s.GetState().StateName()); }

void CDControllerFSMState::Play(CDControllerFSM& s)
  { s.FSMError("Play", s.GetState().StateName()); }

void CDControllerFSMState::End(CDControllerFSM& s)
  { s.FSMError("End", s.GetState().StateName()); }


//----------------------------------------------
// Idle Actions and Transitions
//----------------------------------------------

void CDControllerFSMIdleState::Play( CDControllerFSM& s )
{
    s.SetState(CDControllerFSM::Playing);
}

//----------------------------------------------
// Playing Actions and Transitions
//----------------------------------------------


void CDControllerFSMPlayingState::Stop( CDControllerFSM& s )
{
    s.SetState(CDControllerFSM::Idle);
}


void CDControllerFSMPlayingState::End( CDControllerFSM& s )
{
    s.SetState(CDControllerFSM::Idle);
}

//----------------------------------------------
// State Machine Constructor: CDControllerFSM
//  set Initial State to: Idle
//
//----------------------------------------------
CDControllerFSM::CDControllerFSM() 
 : itsState(&Idle)
{
}

=======================================

This code was generated by SMC from the following input file:

FSMName CDControllerFSM
Context CDController
Initial Idle
FSMGenerator smc.generator.cpp.SMCppGenerator
{
  Idle
  {
    Play  Playing  {}
  }

  Playing
  {
    Stop  Idle  {}
    End   Idle  {}
  }
}

You can get SMC from the resources section of www.objectmentor.com





Robert C. Martin    | "Uncle Bob"              | Software Consultants
Object Mentor Inc.  | rmartin@objectmentor.com | We'll help you get
PO Box 5757         | Tel: (800) 338-6716      | your projects done.
565 Lakeview Pkwy   | Fax: (847) 573-1658      | www.objectmentor.com
Suite 135           |                          | www.XProgramming.com
Vernon Hills, IL,   | Training and Mentoring   | www.junit.org
60061               | OO, XP, Java, C++, Python|

"One of the great commandments of science is:
    'Mistrust arguments from authority.'" -- Carl Sagan
Technical Details
Message-ID: <vc0u4uk5ldcmdnmor191fje44f2b64r0rq@4ax.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-post-01!supernews.com!news.supernews.com!not-for-mail
References: <EMp38.5270$ka7.1036680@news6-win.server.ntlworld.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 114af8,37b747ce5c3c8994
X-Google-Attributes: gid114af8,public
X-Google-Thread: 109fba,37b747ce5c3c8994
X-Google-Attributes: gid109fba,public
X-Google-Thread: 114592,37b747ce5c3c8994
X-Google-Attributes: gid114592,public
X-Google-Thread: 110cb8,37b747ce5c3c8994
X-Google-Attributes: gid110cb8,public
X-Google-Thread: 104db2,37b747ce5c3c8994
X-Google-Attributes: gid104db2,public
X-Google-ArrivalTime: 2002-01-23 11:07:47 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-post-01!supernews.com!news.supernews.com!not-for-mail
From: Robert C. Martin <rmartin @ objectmentor . com>
Newsgroups: alt.comp.lang.learn.c-c++,alt.language.patterns,borland.public.cpp.turbocpp,comp.lang.c++,comp.software.patterns
Subject: Re: conversion to state pattern
Date: Wed, 23 Jan 2002 13:08:06 -0600
Organization: Object Mentor Inc.
Message-ID: <vc0u4uk5ldcmdnmor191fje44f2b64r0rq@4ax.com>
Reply-To: rmartin @ objectmentor . com
References: <EMp38.5270$ka7.1036680@news6-win.server.ntlworld.com>
X-Newsreader: Forte Agent 1.8/32.553
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Complaints-To: newsabuse@supernews.com
Lines: 176