By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,757 Members | 1,990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,757 IT Pros & Developers. It's quick & easy.

State Pattern in C++

P: 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

Jun 17 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
go***************@gmail.com wrote:
>
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).
Jun 17 '07 #2

P: n/a
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ć
Jun 17 '07 #3

P: n/a
On Jun 17, 7:08 pm, John Harrison <john_androni...@hotmail.comwrote:
gordon.is.a.mo...@gmail.com wrote:
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.
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
Jun 18 '07 #4

P: n/a
On Jun 18, 4:15 am, Jurko Gospodneti <mang...@to.avoid.spamwrote:
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
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
Jun 18 '07 #5

P: n/a
In message <11*********************@o11g2000prd.googlegroups. com>,
go***************@gmail.com writes
>On Jun 18, 4:15 am, Jurko Gospodneti <mang...@to.avoid.spamwrote:
> 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.

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
>ended up using CxxTest (which is very nice).
I'll look at this for future reference.
--
Richard Herring
Jun 21 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.