Connecting Tech Pros Worldwide Help | Site Map

State Pattern in C++

  #1  
Old June 17th, 2007, 07:55 AM
gordon.is.a.moron@gmail.com
Guest
 
Posts: n/a


Hello,

I'm implementing a State Pattern in C++, based on the example in the
GoF book. However the example they give only shows a single transition
to another state. In my program I have a choice of states depending on
the input. So, I was going to give each concrete State a small Map of
inputs to States and then find out what transition to make.

However I was wondering if this is a sort of mix between State Table
and State Pattern, but surely you need some conditional code in the
concrete state to decide what state to go to? It just seems that the
State Pattern is designed to eliminate this, yet there is still a need
for it when deciding which state to change to when you have a choice
of multiple state transitions.

Regards,
Gordy

  #2  
Old June 17th, 2007, 08:15 AM
John Harrison
Guest
 
Posts: n/a

re: State Pattern in C++


gordon.is.a.moron@gmail.com wrote:
Quote:
>
Hello,
>
I'm implementing a State Pattern in C++, based on the example in the
GoF book. However the example they give only shows a single transition
to another state. In my program I have a choice of states depending on
the input. So, I was going to give each concrete State a small Map of
inputs to States and then find out what transition to make.
>
However I was wondering if this is a sort of mix between State Table
and State Pattern, but surely you need some conditional code in the
concrete state to decide what state to go to? It just seems that the
State Pattern is designed to eliminate this, yet there is still a need
for it when deciding which state to change to when you have a choice
of multiple state transitions.
>
Regards,
Gordy
>
I don't think the state pattern says anything about how you manage
transitions. It's about how you represent states, namely each state is a
class, and therefore all logic pertaining to that class is in one place
(including its transitions to other states). As you say, you need some
conditional code somewhere.

BTW, this has nothing much to do with C++ so is off-topic here. Try an
OO design group. (comp.objects maybe, not sure).
  #3  
Old June 17th, 2007, 05:25 PM
=?UTF-8?B?SnVya28gR29zcG9kbmV0acSH?=
Guest
 
Posts: n/a

re: State Pattern in C++


Hi.
Quote:
I'm implementing a State Pattern in C++
Have a look at the Boost StateChart library. It is included
in the latest 1.34 Boost library release and we found it quite
useful for cleaning up some of our state-machine code.

Hope this helps.

Best regards,
Jurko Gospodnetić
  #4  
Old June 18th, 2007, 10:25 AM
gordon.is.a.moron@gmail.com
Guest
 
Posts: n/a

re: State Pattern in C++


On Jun 17, 7:08 pm, John Harrison <john_androni...@hotmail.comwrote:
Quote:
gordon.is.a.mo...@gmail.com wrote:
>
Quote:
Hello,
>
Quote:
I'm implementing a State Pattern in C++, based on the example in the
GoF book. However the example they give only shows a single transition
to another state. In my program I have a choice of states depending on
the input. So, I was going to give each concrete State a small Map of
inputs to States and then find out what transition to make.
>
Quote:
However I was wondering if this is a sort of mix between State Table
and State Pattern, but surely you need some conditional code in the
concrete state to decide what state to go to? It just seems that the
State Pattern is designed to eliminate this, yet there is still a need
for it when deciding which state to change to when you have a choice
of multiple state transitions.
>
Quote:
Regards,
Gordy
>
I don't think the state pattern says anything about how you manage
transitions. It's about how you represent states, namely each state is a
class, and therefore all logic pertaining to that class is in one place
(including its transitions to other states). As you say, you need some
conditional code somewhere.
>
Good point, I think they pretty much say this in the GoF, but I wasn't
sure.
Quote:
BTW, this has nothing much to do with C++ so is off-topic here. Try an
OO design group. (comp.objects maybe, not sure).
Oops, sorry.

Gordy


  #5  
Old June 18th, 2007, 10:25 AM
gordon.is.a.moron@gmail.com
Guest
 
Posts: n/a

re: State Pattern in C++


On Jun 18, 4:15 am, Jurko Gospodneti <mang...@to.avoid.spamwrote:
Quote:
Hi.
>
Quote:
I'm implementing a State Pattern in C++
>
Have a look at the Boost StateChart library. It is included
in the latest 1.34 Boost library release and we found it quite
useful for cleaning up some of our state-machine code.
>
Hope this helps.
>
Best regards,
Jurko Gospodneti
Funnily enough I was looking at Boost, but I think it's Linux only,
ended up using CxxTest (which is very nice).
I'll look at this for future reference.

Regards,
Gordy


  #6  
Old June 21st, 2007, 12:35 PM
Richard Herring
Guest
 
Posts: n/a

re: State Pattern in C++


In message <1182158284.762170.93010@o11g2000prd.googlegroups. com>,
gordon.is.a.moron@gmail.com writes
Quote:
>On Jun 18, 4:15 am, Jurko Gospodneti <mang...@to.avoid.spamwrote:
Quote:
> Hi.
>>
Quote:
I'm implementing a State Pattern in C++
>>
> Have a look at the Boost StateChart library. It is included
>in the latest 1.34 Boost library release and we found it quite
>useful for cleaning up some of our state-machine code.
>>
> Hope this helps.
>
>Funnily enough I was looking at Boost, but I think it's Linux only,
Why do you think that? Much of it is platform-independent template code,
and for those parts that do need run-time libraries, tools are provided
for building them on a variety of platforms and toolsets. See these
pages:

http://boost.org/more/getting_started/windows.html
http://boost.org/more/getting_starte...-variants.html
Quote:
>ended up using CxxTest (which is very nice).
>I'll look at this for future reference.
--
Richard Herring
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
state pattern / template pcrepieux answers 6 September 27th, 2007 08:45 PM
The Interpreter Pattern in PHP 5 ltruett@fluffycat.com answers 1 August 29th, 2006 01:25 AM
The Observer Pattern in PHP 5 FluffyCat answers 0 May 9th, 2006 12:25 AM
MVC-Pattern in my C# GUI !?! Marcel Hug answers 10 January 5th, 2006 06:05 PM