Implementation of Simple Finite State Machine (FSM)

Source: Internet
Author: User
Tags fsm

FSM interface in general gate mode,

//-------------------------------------------
# Include "igatestate. H"
# Include <assert. h>
# Include <iostream. h>
//-------------------------------------------

//-------------------------------------------
Class igatefsm
{
Public:
Igatefsm ();
Virtual ~ Igatefsm ();

Public:

Inline void setstate (igatestate * State) {_ state = State ;}

Virtual void onevent (enum_event event );
 
PRIVATE:
Igatestate * _ state;
};
//-------------------------------------------

// The Implementation of the derived class provides business-related methods such as lock and unlock Based on the metro cross Model

//-------------------------------------------

Class vgatefsm: Public igatefsm
{
Public:
Vgatefsm (){}
Virtual ~ Vgatefsm (){}

Public:

Void lock ();
Void unlock ();
Void alarm ();
Void thanks ();

PRIVATE:
Bool m_islock;

};
//-------------------------------------------

// ========================================
// 'Indicates the abstract interface of the Status class
Class igatefsm;

// Event description
Enum enum_event
{
Event_coin, // í~è ~ó² ± ~~~â ~~~~
Event_pass // 'invalid parameter *â invalid Parameter
};

Class igatestate
{
Public:
Igatestate ();
Virtual ~ Igatestate ();

Public:
 
Virtual void onevent (enum_event event, igatefsm * FSM) = 0;

};
//------------------------------------------

// Implementation class in state mode, which represents two States

Class vgatelock: Public igatestate
{
Public:
Static igatestate * instance ();

Public:
Virtual void onevent (enum_event event, igatefsm * FSM );
 
PRIVATE:
Vgatelock (){}

Static vgatelock * _ instance;

};
//------------------------------------------
Class vswitchunlock: Public igatestate
{
Public:
Static igatestate * instance ();

Public:
Virtual void onevent (enum_event event, igatefsm * FSM );

PRIVATE:
Vgateunlock (){}

Static vgateunlock * _ instance;

};

// FSM implementation

//-------------------------------------------
Igatefsm: igatefsm ()
{
_ State = NULL;
}
Igatefsm ::~ Igatefsm ()
{
}
//-------------------------------------------
Void igatefsm: onevent (enum_event event)
{
Assert (_ state! = NULL );

_ State-> onevent (event, this );
}
//-------------------------------------------
Void vgatefsm: Lock ()
{
This-> m_islock = true;

Cout <"lock the gate." <Endl;
}
//-------------------------------------------
Void vgatefsm: Unlock ()
{
This-> m_islock = true;
 
Cout <"unlock the gate." <Endl;
}
//-------------------------------------------
Void vgatefsm: Alarm ()
{
Cout <"You shoshould insert a coin." <Endl;
}
//-------------------------------------------
Void vgatefsm: Thanks ()
{
Cout <"thank you for pay again." <Endl;
}
//-------------------------------------------

// Implementation of State

Vgatelock * vgatelock: _ instance = NULL;
Vgateunlock * vgateunlock: _ instance = NULL;
Void vgatelock: onevent (enum_event event, igatefsm * FSM)
{
Vgatefsm * vfsm = (vgatefsm *) FSM;

If (event = event_pass)
{
Vfsm-> alarm ();
Return;
}
 
If (event = event_coin)
Vfsm-> unlock ();

Vfsm-> setstate (vswitchunlock: instance ());

}
//-------------------------------------------
Void vgateunlock: onevent (enum_event event, igatefsm * FSM)
{
Vgatefsm * vfsm = (vgatefsm *) FSM;

If (event = event_coin)
{
Vfsm-> thanks ();
Return;
}
 
If (event = event_pass)
Vfsm-> lock ();

Vfsm-> setstate (vgatelock: instance ());
}
//-------------------------------------------
Igatestate * vgatelock: instance ()
{
If (_ instance = NULL)
_ Instance = new vgatelock ();

Return _ instance;
}
//-------------------------------------------
Igatestate * vswitchunlock: instance ()
{
If (_ instance = NULL)
_ Instance = new vswitchunlock ();

Return _ instance;
}
//-------------------------------------------

Test procedure:

Int main (INT argc, char * argv [])
{
Printf ("FSM Demo:/n ");

Vgatefsm * _ FSM = new vgatefsm ();

_ FSM-> setstate (vgatelock: instance ());

_ FSM-> onevent (event_pass );
_ FSM-> onevent (event_coin );
_ FSM-> onevent (event_coin );
_ FSM-> onevent (event_pass );

Delete _ FSM;

Printf ("/N ");
Return 0;
}

The output is as follows:

You shoshould insert a coin.

Unlock the gate

Thank you for pay again.

Lock the gate.

Same as expected

Summary:

Finite State Machine is a modeling method in OO. It analyzes and solves problems from the perspective of State objects. The most important thing is to set up abstract state classes and related implementation classes

To implement the conversion in the derived class, so as to achieve better separation of logic and implementation.

In addition, the structure is similar to that of strategy and observer. It seems that the mode does not have to be so dead. In fact, they all have similarities ..

Why can't I paste UML images? Strange.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.