State pattern question

M

Mikael Petterson

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/JAvaResources/DesignPatterns/StatePat.htm

Any ideas how we can implements this?

cheers,

//mike
 
D

Daniel Pitts

Mikael said:
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.
 
R

Roedy Green

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top