State Machine and Design Pattern

W

wudehui2006

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!
 
A

alfps

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
 
J

jason.cipriani

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
 
M

Michael DOUBEZ

alfps a écrit :
Singleton design !?! What a beautiful expression :)

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 ?).
 
A

AnonMail2005

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
 
M

ma740988

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?
 
K

KjellKod

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
 
J

jason.cipriani

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
 
J

jason.cipriani

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
 
M

ma740988

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!
 
J

jason.cipriani

Got it!


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!

Yes, a map of IDs to callback objects is one way of implementing it.
For that, you would use a map of IDs to callback objects. The factory
model is for creation of objects, it doesn't apply.

The nonsense question I gave as an example was meant to make the point
that you are asking the wrong questions. A "finite state machine" is a
construct whose implementation is not relevant to a type of software
design pattern. A more relevant example question might have been "I
want to implement a red-black tree, should I use the factory model?".
The question simply does not make sense.

Jason
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top