State pattern question - Java

This is a discussion on State pattern question - Java ; Hi, We use the state pattern to get rid of plenty of if-if else -statements. In each concrete state we need to access id's that can be created in any state. All concrete states ( for this conntext) needs to ...

+ Reply to Thread
Results 1 to 4 of 4

State pattern question

  1. Default State pattern question

    Hi,

    We use the state pattern to get rid of plenty of if-if else -statements.
    In each concrete state we need to access id's that can be created in any
    state. All concrete states ( for this conntext) needs to access id's.

    I don't want to create an instance for containing id's and pass that in the
    constructor for each concreate state.

    Here is a link for the pattern:

    http://www.exciton.cs.rice.edu/JAvaR...s/StatePat.htm

    Any ideas how we can implements this?

    cheers,

    //mike

  2. Default Re: State pattern question

    Mikael Petterson wrote:
    > Hi,
    >
    > We use the state pattern to get rid of plenty of if-if else -statements.
    > In each concrete state we need to access id's that can be created in any
    > state. All concrete states ( for this conntext) needs to access id's.
    >
    > I don't want to create an instance for containing id's and pass that in the
    > constructor for each concreate state.

    Don't use IDs, let the object identity itself be the "id" of the state.
    Thats one of the points of using the State pattern.

    class StateFoo implements State {
    public State doSomething() {
    return new StateBar();
    }
    }

    class StateBar implements State {
    public State doSomething() {
    return new StateFoo();
    }
    }

    Hope this helps. If not, please clarify your problem with a short example.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

  3. Default Re: State pattern question

    Daniel Pitts wrote:
    > Hope this helps. If not, please clarify your problem with a short example.


    I.e.,
    <http://www.physci.org/codes/sscce.html>

    --
    Lew

  4. Default Re: State pattern question

    On Thu, 11 Oct 2007 09:02:40 +0200, Mikael Petterson
    <mikael.petterson@ericsson.com> wrote, quoted or indirectly quoted
    someone who said :

    >Any ideas how we can implements this?


    Consider an enum. Each state is an enum constant. Each constant can
    be initialised with magic values for that state. Each state can also
    have custom implementations of various methods.

    Your state tracker is a pointer to an enum constant.

    Have a look at the finite state machines in the parsers used in the
    JDisplay package. See http://mindprod.com/products1.html#JDISPLAY

    see http://mindprod.com/jgloss/enum.html
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com

+ Reply to Thread

Similar Threads

  1. Replies: 1
    Last Post: 11-28-2007, 10:40 PM
  2. state pattern / template
    By Application Development in forum c++
    Replies: 6
    Last Post: 09-27-2007, 02:35 PM
  3. State Pattern in C++
    By Application Development in forum c++
    Replies: 5
    Last Post: 06-21-2007, 06:33 AM
  4. GOF State Pattern without fully shared interface
    By Application Development in forum Theory and Concepts
    Replies: 64
    Last Post: 05-09-2007, 02:10 PM
  5. ASP.NET 2.0 session state question
    By Application Development in forum CSharp
    Replies: 0
    Last Post: 03-12-2005, 01:43 AM