Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

State pattern question

Thread view: 
Mikael Petterson - 11 Oct 2007 08:02 GMT
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
Daniel Pitts - 11 Oct 2007 15:24 GMT
> Hi,
>
[quoted text clipped - 4 lines]
> 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.

Signature

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

Lew - 11 Oct 2007 17:04 GMT
> Hope this helps. If not, please clarify your problem with a short example.

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

Signature

Lew

Roedy Green - 11 Oct 2007 21:18 GMT
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
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.