473,394 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

State Pattern in C++



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
5 4017
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Samuel R. Neff | last post by:
Is there any downside to using the Memento pattern to store the current state of an object instead of using private fields for internal state and a memento just for some bookmarked state? Seems...
4
by: Shawnk | last post by:
This post is intended to verify that true value semantics DO NOT EXIST for the Enum class (relative to boolean operations). If this is true then (thus and therefore) you can not design state...
1
by: ltruett | last post by:
Last week I continued my series of design patterns examples using PHP 5 with the Bridge Pattern, Flyweight Pattern, and Proxy Pattern. Here now is my 20th PHP 5 design pattern example, the...
0
by: stephen.nil | last post by:
http://code.google.com/p/spxml/ spxml is a xml parser, supports PULL-model and DOM-model xml parsing. Using state pattern make it simple to implement the xml parser. State pattern has three...
67
by: Rui Maciel | last post by:
I've been delving into finite state machines and at this time it seems that the best way to implement them is through an intense use of the goto statement. Yet, everyone plus their granmother is...
1
by: Steven P | last post by:
Hi, In my project I have a timer-driven state machine. The timer object are in the form. Update of controls like edit, label will be executed in some states. The state machine is in a seperate...
4
weaknessforcats
by: weaknessforcats | last post by:
Design Patterns – State Often computer software operates based on a condition called a state. These states traditionally have been implemented using a switch statement. The cases of the switch...
6
by: pcrepieux | last post by:
Hi, I recently meet a problem while "playing" with the state pattern. I was wondering if each of the member function dedicated to handle events open(), close(), ack() could be change to...
3
by: Pallav singh | last post by:
Hi All i am getting Error while writing following code for state design Pattern kindly let me know How to Correct this Error ?? Thanks Pallav Singh ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.