Connecting Tech Pros Worldwide Forums | Help | Site Map

State Pattern in C++

gordon.is.a.moron@gmail.com
Guest
 
Posts: n/a
#1: Jun 17 '07


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


John Harrison
Guest
 
Posts: n/a
#2: Jun 17 '07

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).
=?UTF-8?B?SnVya28gR29zcG9kbmV0acSH?=
Guest
 
Posts: n/a
#3: Jun 17 '07

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ć
gordon.is.a.moron@gmail.com
Guest
 
Posts: n/a
#4: Jun 18 '07

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


gordon.is.a.moron@gmail.com
Guest
 
Posts: n/a
#5: Jun 18 '07

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


Richard Herring
Guest
 
Posts: n/a
#6: Jun 21 '07

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