Article View: alt.comp.lang.learn.c-c++
Article #458conversion to state pattern
From: Max Coppin
Date: Wed, 23 Jan 2002 03:06
Date: Wed, 23 Jan 2002 03:06
167 lines
5325 bytes
5325 bytes
Hi,
Sorry for previous binary posting.
Below is the code I need to convert to the state pattern
This is also availiable for download from www.stmikes.co.uk/max - I hope!
If you can't get the files and would like them please e-mail
maxcoppin@btinternet.com
I really appreciate it!
Thanks people!
Max
main.cpp:-
#include "cdcontroller.h"
#include <iostream.h>
#include <conio.h>
void main()
{
cdController cd;
cout <<endl << "Event Play when in Idle state" <<endl;
cd.processEvent(cdController::PLAY);
cout << cd.getStatus() <<endl;
cout <<endl << "Event Stop when in Playing state"<<endl;
cd.processEvent(cdController::STOP);
cout << cd.getStatus() <<endl;
cout <<endl << "Event Stop when in Idle state"<<endl;
cd.processEvent(cdController::STOP);
cout << cd.getStatus() <<endl;
cout <<endl << "Event Play when in Idle state"<<endl;
cd.processEvent(cdController::PLAY);
cout << cd.getStatus() <<endl;
cout <<endl << "Event End_Of_Last_Track when in Play state"<<endl;
cd.processEvent(cdController::END_OF_LAST_TRACK);
cout << cd.getStatus() <<endl;
getch();
}
cdcontroller.h:-
// cdController.h: interface for the cdController class.
//
//////////////////////////////////////////////////////////////////////
#if
!defined(AFX_CDCONTROLLER_H__B8972782_F2D4_11D5_84B5_EEF8B4A7605C__INCLUDED_
)
#define AFX_CDCONTROLLER_H__B8972782_F2D4_11D5_84B5_EEF8B4A7605C__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <afx.h>
class cdController
{
public:
//Enumerated types
enum EState {IDLE ,PLAYING};
enum EEvent {PLAY, STOP,END_OF_LAST_TRACK};
void processEvent(EEvent e);
cdController();
virtual ~cdController();
CString getStatus();
private:
CString stateToCString(EState s);
CString eventToCString(EEvent e);
void changeState(EState s);
EState _state, _prevState;
EEvent _event;
};
#endif //
!defined(AFX_CDCONTROLLER_H__B8972782_F2D4_11D5_84B5_EEF8B4A7605C__INCLUDED_
)
cdcontroller.cpp:-
// cdController.cpp: implementation of the cdController class.
//
//////////////////////////////////////////////////////////////////////
#include "cdController.h"
//#include <iostream.h>
#include <afxtempl.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
cdController::cdController()
{
//set initial state
_state = IDLE;
}
cdController::~cdController()
{
}
void cdController::processEvent(EEvent e)
{
_event = e;
_prevState = _state;
switch (_state)
{
case IDLE : if (e == PLAY)
changeState(PLAYING);
break;
case PLAYING : switch (e)
{
case STOP : changeState(IDLE);break;
case END_OF_LAST_TRACK : changeState(IDLE);break;
}
}
}
void cdController::changeState(EState s)
{
_state = s;
}
CString cdController::getStatus()
{ CString s;
s="Previous state : " + stateToCString(_prevState) + "\t"
+ "Event : " + eventToCString(_event) + "\t"
+ "Current state : " + stateToCString(_state);
return s;
}
CString cdController::stateToCString(EState s)
{
//Have to convert enumerated type to CString
//so that it can be output to the screen as text
switch (s)
{
case IDLE : return "IDLE"; break;
case PLAYING : return "PLAYING"; break;
default : return "UNKOWN STATE";
}
}
CString cdController::eventToCString(EEvent e)
{
//Have to convert enumerated type to CString
//so that it can be output to the screen as text
switch (e)
{
case PLAY : return "PLAY"; break;
case STOP : return "STOP"; break;
case END_OF_LAST_TRACK : return "END_OF_LAST_TRACK"; break;
default : return "UNKOWN EVENT";
}
}
Technical Details
Message-ID:
<EMp38.5270$ka7.1036680@news6-win.server.ntlworld.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!supernews.com!isdnet!btnet-peer1!btnet-peer0!btnet!news5-gui.server.ntli.net!ntli.net!news6-win.server.ntlworld.com.POSTED!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 114af8,37b747ce5c3c8994,start X-Google-Attributes: gid114af8,public X-Google-Thread: 110cb8,37b747ce5c3c8994,start X-Google-Attributes: gid110cb8,public X-Google-Thread: 109fba,37b747ce5c3c8994,start X-Google-Attributes: gid109fba,public X-Google-Thread: 114592,37b747ce5c3c8994,start X-Google-Attributes: gid114592,public X-Google-Thread: 104db2,37b747ce5c3c8994,start X-Google-Attributes: gid104db2,public X-Google-ArrivalTime: 2002-01-22 19:11:21 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!supernews.com!isdnet!btnet-peer1!btnet-peer0!btnet!news5-gui.server.ntli.net!ntli.net!news6-win.server.ntlworld.com.POSTED!not-for-mail From: "Max Coppin" <maxcoppin@btinternet.com> Newsgroups: alt.comp.lang.learn.c-c++,alt.language.patterns,borland.public.cpp.turbocpp,comp.lang.c++,comp.software.patterns Subject: conversion to state pattern Lines: 170 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 Message-ID: <EMp38.5270$ka7.1036680@news6-win.server.ntlworld.com> Date: Wed, 23 Jan 2002 03:06:10 -0000 NNTP-Posting-Host: 80.0.2.203 X-Complaints-To: abuse@ntlworld.com X-Trace: news6-win.server.ntlworld.com 1011755172 80.0.2.203 (Wed, 23 Jan 2002 03:06:12 GMT) NNTP-Posting-Date: Wed, 23 Jan 2002 03:06:12 GMT Organization: ntl Cablemodem News Service