Hi friends, Can anyone explain the State Pattern. Thanx, Vidya
Kyle Brown
author
Ranch Hand
Joined: Aug 10, 2001
Posts: 3879
posted
0
Well, the best place to start would be the Gang of Four book ("Design Patterns: Elements of Reusable Object-Oriented Design") which was the first description of the pattern. Another try at explaining it can be found here: http://hometown.aol.com/kgb1001001/Companion/Companion.htm and another is here: http://www.enteract.com/~bradapp/javapats.html#Behavioral or you can get Mark Grand's book, or Alan Shalloway's book , or Jim Cooper's book or you can go look at the earlier thread comparing State and Strategy in this forum. Having said all of that, is there a particular question or misunderstanding ABOUT the State pattern that you have? Kyle ------------------ Kyle Brown, Author of Enterprise Java (tm) Programming with IBM Websphere See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information. [This message has been edited by Kyle Brown (edited October 06, 2001).] [This message has been edited by Kyle Brown (edited October 06, 2001).] [This message has been edited by Kyle Brown (edited October 06, 2001).] [This message has been edited by Kyle Brown (edited October 06, 2001).]
Alan Shalloway.<BR>Look for Jim Trott and my book: <A HREF="http://www.amazon.com/exec/obidos/ASIN/0201715945/ref=ase_electricporkchop/103-0514572-3811868" TARGET=_blank rel="nofollow">Design Patterns Explained</A><BR>Visit our site <A HREF="http://www.netobjectives.com" TARGET=_blank rel="nofollow">Net Objectives</A>.<BR>Visit our <A HREF="http://www.netobjectives.com/dpexplained/index.html" TARGET=_blank rel="nofollow">Design Patterns Explained Community of Practice</A><BR>Check out our <A HREF="http://www.netobjectives.com/xml/xml_cdrom_info.htm" TARGET=_blank rel="nofollow">CDROM based audio training in XML</A>
Kyle Brown
author
Ranch Hand
Joined: Aug 10, 2001
Posts: 3879
posted
0
Mea Culpa, Alan. I could have swore from your discussion on another thread that you said that it had -- oh well. Sounds like you've got a "volume II- more patterns experiences" to come, then Personally, I can't claim knowledge from your book directly yet, I'm waiting for Amazon to ship my copy Kyle ------------------ Kyle Brown, Author of Enterprise Java (tm) Programming with IBM Websphere See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Adeyemi Adegbile
Greenhorn
Joined: Oct 08, 2001
Posts: 8
posted
0
The State pattern is defined in GOF as "Allow an object to alter its behavior when its internal state changes. The object will appear to change its class" This is clearly explained in the example given by the code below taken from thinking in Patterns //: c04:statemachine:StateMachineDemo.java // From 'Thinking in Patterns (with Java)' by Bruce Eckel // www.BruceEckel.com. See copyright notice in CopyRight.txt. // Demonstrates StateMachine pattern // and Template method. package c04.statemachine; import java.util.*; import com.bruceeckel.test.*; // Adeyemi Adegbile Comment added: State represented as a // interface interface State { void run(); } abstract class StateMachine { protected State currentState; abstract protected boolean changeState(); // Template method: protected final void runAll() { while(changeState()) // Customizable currentState.run(); } } // A different subclass for each state: // Adeyemi Adegbile Comment added: ConcreteState subclasses // according to GOF class Wash implements State { public void run() { System.out.println("Washing"); } } // Adeyemi Adegbile Comment added: ConcreteState subclasses // according to GOF class Spin implements State { public void run() { System.out.println("Spinning"); } } // Adeyemi Adegbile Comment added: ConcreteState subclasses // according to GOF class Rinse implements State { public void run() { System.out.println("Rinsing"); } } class Washer extends StateMachine { private int i = 0; // The state table: private State states[] = { new Wash(), new Spin(), new Rinse(), new Spin(), }; public Washer() { runAll(); } public boolean changeState() { if(i < states.length) { // Change the state by setting the // surrogate reference to a new object: currentState = states[i++]; return true; } else return false; } } public class StateMachineDemo extends UnitTest { Washer w = new Washer(); public void test() { // The constructor does the work. // This just makes sure it will complete // without throwing an exception. } public static void main(String args[]) { new StateMachineDemo().test(); } } This example clearly demonstracte the state pattern. According to GOF the pattern, the participants are Context, this will be the class to set the the current state State this is the abstract class wash which go through different state. ConcreteState subclasses : One of this class will be initantiated as run time depending on the status of the class. State objects are often Singletons (127).GOF. Adeyemi Adegbile
Originally posted by Iyengar Srividya76: Hi friends, Can anyone explain the State Pattern. Thanx, Vidya
Iyengar Srividya76
Ranch Hand
Joined: Sep 09, 2001
Posts: 87
posted
0
Hi.. Thanx for yr explanation... Can u explain just the statement.... "Allow an object to alter its behavior when its internal state changes. The object will appear to change its class" Thanx, Vidya
Kyle Brown
author
Ranch Hand
Joined: Aug 10, 2001
Posts: 3879
posted
0
What that statement means is that if you send a message to the context object (the one holding the state) while it holds one state object (State "A") then it will respond to the message in one way. If you send the same message at a later time while the context holds a different state object (State "B") it will respond to the message in a different way. As the GOF book clearly shows, this is because the messages will be delegated to the state object. Kyle ------------------ Kyle Brown, Author of Enterprise Java (tm) Programming with IBM Websphere See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.