State Machine and Design Pattern

Discussion in 'C++' started by wudehui2006@gmail.com, Dec 23, 2008.

  1. Guest

    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!
     
    , Dec 23, 2008
    #1
    1. Advertising

  2. alfps Guest

    On 23 Des, 06:19, 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
     
    alfps, Dec 23, 2008
    #2
    1. Advertising

  3. Guest

    On Dec 23, 12:19 am, 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
     
    , Dec 23, 2008
    #3
  4. alfps a écrit :
    > On 23 Des, 06:19, 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
     
    Michael DOUBEZ, Dec 23, 2008
    #4
  5. Guest

    On Dec 23, 12:19 am, 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
     
    , Dec 23, 2008
    #5
  6. ma740988 Guest

    On Dec 23, 4:01 am, ""
    <> 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?
     
    ma740988, Dec 24, 2008
    #6
  7. KjellKod Guest

    On Dec 24, 9:32 am, ma740988 <> wrote:
    > On Dec 23, 4:01 am, ""
    >
    > <> 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, "" <>
    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.
     
    KjellKod, Dec 24, 2008
    #7
  8. Guest

    On Dec 24, 10:32 am, ma740988 <> wrote:
    > On Dec 23, 4:01 am, ""
    >
    > <> 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
     
    , Dec 25, 2008
    #8
  9. Guest

    On Dec 24, 9:17 pm, ""
    <> wrote:
    > On Dec 24, 10:32 am, ma740988 <> wrote:
    >
    > > On Dec 23, 4:01 am, ""

    >
    > > <> 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
     
    , Dec 25, 2008
    #9
  10. ma740988 Guest

    On Dec 24, 9:17 pm, ""
    <> 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!
     
    ma740988, Dec 25, 2008
    #10
  11. Guest

    On Dec 24, 10:42 pm, ma740988 <> wrote:
    > On Dec 24, 9:17 pm, ""
    >
    >
    >
    > <> 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!


    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
     
    , Dec 28, 2008
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sunny
    Replies:
    1
    Views:
    467
    Salt_Peter
    Dec 7, 2006
  2. Grumps
    Replies:
    2
    Views:
    692
    Grumps
    Feb 13, 2008
  3. Pallav singh
    Replies:
    0
    Views:
    364
    Pallav singh
    Jan 22, 2012
  4. Pallav singh
    Replies:
    0
    Views:
    405
    Pallav singh
    Jan 22, 2012
  5. Pallav singh
    Replies:
    1
    Views:
    453
    Peter Remmers
    Jan 22, 2012
Loading...

Share This Page