State Pattern in C++

G

gordon.is.a.moron

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
 
J

John Harrison

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).
 
G

Guest

Hi.
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ć
 
G

gordon.is.a.moron

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.
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
 
G

gordon.is.a.moron

Hi.


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top