Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > State Machine and Design Pattern

Reply
Thread Tools

State Machine and Design Pattern

 
 
wudehui2006@gmail.com
Guest
Posts: n/a
 
      12-23-2008
Hi I have a question. I have implemented a state machine by singleton
design. Now I change my algorithm, so I will have two state machines
which transtions diagrams are about the same, however at each state
they perform different functions. I could have two classes, with the
same transtion but different functions for each states, but it sounds
so stupid. What kind of design patterns are good for this situation?
Thank you very much!
 
Reply With Quote
 
 
 
 
alfps
Guest
Posts: n/a
 
      12-23-2008
On 23 Des, 06:19, (E-Mail Removed) wrote:
> Hi I have a question. I have implemented a state machine by singleton
> design. Now I change my algorithm, so I will have two state machines
> which transtions diagrams are about the same, however at each state
> they perform different functions. I could have two classes, with the
> same transtion but different functions for each states, but it sounds
> so stupid. What kind of design patterns are good for this situation?
> Thank you very much!


Most every computer science problem can be solved by applying
indirection.

Cheers & hth.,

- Alf
 
Reply With Quote
 
 
 
 
jason.cipriani@gmail.com
Guest
Posts: n/a
 
      12-23-2008
On Dec 23, 12:19*am, (E-Mail Removed) wrote:
> I have implemented a state machine by singleton
> design.


Huh? What do state machines and singleton classes have to do with each
other?

> Now I change my algorithm, so I will have two state machines
> which transtions diagrams are about the same, however at each state
> they perform different functions.


Ok.

> I could have two classes, with the
> same transtion but different functions for each states, but it sounds
> so stupid. What kind of design patterns are good for this situation?


What are you asking? The best "design pattern" for this situation is
the old "implement-something-that-lets-me-use-the-same-state-
transition-tables-but-with-different-state-actions" idiom. Maybe put
the state machine in some base class and let derived classes implement
the functions at each state, maybe a class that takes a transition
table + a list of callback objects or functions to call at each state,
maybe two separate classes with the same transition but different
functions for each state, maybe any one of a million ways you have to
solve the problem. None of them are "design patterns" though -- you
are asking the wrong question...

Jason

 
Reply With Quote
 
Michael DOUBEZ
Guest
Posts: n/a
 
      12-23-2008
alfps a écrit :
> On 23 Des, 06:19, (E-Mail Removed) wrote:
>> Hi I have a question. I have implemented a state machine by singleton
>> design.


Singleton design !?! What a beautiful expression

>> Now I change my algorithm, so I will have two state machines
>> which transtions diagrams are about the same, however at each state
>> they perform different functions. I could have two classes, with the
>> same transtion but different functions for each states, but it sounds
>> so stupid. What kind of design patterns are good for this situation?
>> Thank you very much!


Have you looked up "hierarchical state machine" ? There are also other
ways but it depends on how you balance your design.

> Most every computer science problem can be solved by applying
> indirection.


And iteratively (or is it a well founded induction ?).

--
Michael
 
Reply With Quote
 
AnonMail2005@gmail.com
Guest
Posts: n/a
 
      12-23-2008
On Dec 23, 12:19*am, (E-Mail Removed) wrote:
> Hi I have a question. I have implemented a state machine by singleton
> design. Now I change my algorithm, so I will have two state machines
> which transtions diagrams are about the same, however at each state
> they perform different functions. I could have two classes, with the
> same transtion but different functions for each states, but it sounds
> so stupid. What kind of design patterns are good for this situation?
> Thank you very much!


I suggest taking a look at the book "Head First Design Patterns". The
head first is a series of books by oreilly publishing. In it, they
motivate and describe in detail some of the most widely used design
patterns from scratch. From there you will learn exactly what a state
machine is and also how to implement it in an object oriented manner.
I highly recommend it.

HTH
 
Reply With Quote
 
ma740988
Guest
Posts: n/a
 
      12-24-2008
On Dec 23, 4:01 am, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:
> maybe a class that takes a transition
> table + a list of callback objects or functions to call at each state,


I like this idea, but this sounds like the factory pattern. Am I
mistaken?
 
Reply With Quote
 
KjellKod
Guest
Posts: n/a
 
      12-24-2008
On Dec 24, 9:32 am, ma740988 <(E-Mail Removed)> wrote:
> On Dec 23, 4:01 am, "(E-Mail Removed)"
>
> <(E-Mail Removed)> wrote:
> > maybe a class that takes a transition
> > table + a list of callback objects or functions to call at each state,

>
> I like this idea, but this sounds like the factory pattern. Am I
> mistaken?


Factory patterns is about creating objects. What Jason wrote above
doesn't sound like me like purpose would be to create objects but how
to call the appropriate functions at the current state.

I do concur with a previous writer
On Dec 23, 8:54 am, "(E-Mail Removed)" <(E-Mail Removed)>
wrote:
> I suggest taking a look at the book "Head First Design Patterns".
>...
> From there you will learn exactly what a state
> machine is and also how to implement it in an object oriented manner.



 
Reply With Quote
 
jason.cipriani@gmail.com
Guest
Posts: n/a
 
      12-25-2008
On Dec 24, 10:32*am, ma740988 <(E-Mail Removed)> wrote:
> On Dec 23, 4:01 am, "(E-Mail Removed)"
>
> <(E-Mail Removed)> wrote:
> > maybe a class that takes a transition
> > table + a list of callback objects or functions to call at each state,

>
> I like this idea, but this sounds like the factory pattern. *Am I
> mistaken?


Yes, you are mistaken. The "factory pattern" refers to an object
creation method where you have a "factory" object that knows how to
create new instances of an object. That has nothing to do with what I
said.

The mistake here is associating design patterns with basic tasks.
Again, it is not the write question to be asking. Questions like this
do not make sense: "I want to write an internet chat program, should I
use a factory model?"

Jason
 
Reply With Quote
 
jason.cipriani@gmail.com
Guest
Posts: n/a
 
      12-25-2008
On Dec 24, 9:17*pm, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:
> On Dec 24, 10:32*am, ma740988 <(E-Mail Removed)> wrote:
>
> > On Dec 23, 4:01 am, "(E-Mail Removed)"

>
> > <(E-Mail Removed)> wrote:
> > > maybe a class that takes a transition
> > > table + a list of callback objects or functions to call at each state,

>
> > I like this idea, but this sounds like the factory pattern. *Am I
> > mistaken?

>
> Yes, you are mistaken. The "factory pattern" refers to an object
> creation method where you have a "factory" object that knows how to
> create new instances of an object. That has nothing to do with what I
> said.
>
> The mistake here is associating design patterns with basic tasks.
> Again, it is not the write question to be asking.


Or the right write to be writing!

Jason

> Questions like this
> do not make sense: "I want to write an internet chat program, should I
> use a factory model?"
>
> Jason


 
Reply With Quote
 
ma740988
Guest
Posts: n/a
 
      12-25-2008
On Dec 24, 9:17*pm, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:

> Yes, you are mistaken. The "factory pattern" refers to an object
> creation method where you have a "factory" object that knows how to
> create new instances of an object. That has nothing to do with what I
> said.

Got it!
>
> The mistake here is associating design patterns with basic tasks.
> Again, it is not the write question to be asking. Questions like this
> do not make sense: "I want to write an internet chat program, should I
> use a factory model?"


No idea what the hell you're taking about with regards to creating an
internet chat program using a factory model.

In my mind what you alluded to sounded like a map where the key could
be the id of the table and the mapped value could be the list of
callback objects... that led to thoughts on the factory pattern but I
was mistaken so Peace!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ and design Pattern (Composite design Pattern ) Pallav singh C++ 1 01-22-2012 10:48 PM
C++ and design Pattern (Composite design Pattern ) Pallav singh C++ 0 01-22-2012 10:26 PM
C++ and design Pattern (Composite design Pattern ) Pallav singh C++ 0 01-22-2012 10:25 PM
May I have a example of design pattern of "composite", I still feel fuzzy after reading book of Addison-Wesley's"design pattern " jones9413@yahoo.com C++ 1 08-31-2007 04:09 AM
documents related to factory design pattern and Abstract foctory pattern. sunny C++ 1 12-07-2006 04:26 AM



Advertisments