CS 635 Advanced Object-Oriented Design & Programming Spring Semester, 2001 State |
||
---|---|---|
© 2001, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 29-Mar-01 |
State
Example - SPOP
Simple Post Office Protocol
SPOP is used to download e-mail from a server
SPOP supports the following command:
RETR Command
Arguments: a message-number
Returns: the mail message indicated by the number
QUIT Command
Arguments: none
Updates mail box to reflect transactions taken during the transaction state, then logs user out
If session ends by any method except the QUIT command, the updates are not done
The Switch Statement
class SPop { static final int HAVE_USER_NAME = 2; static final int START = 3; static final int AUTHORIZED = 4; private int state = START; String userName; String password; public void user( String userName ) { switch (state) { case START: { this.userName = userName; state = HAVE_USER_NAME; break; } case HAVE_USER_NAME: case AUTHORIZED:{ endLastSessionWithoutUpdate(); goToStartState() } } }
Implementation with Switch Statement Cont.
public void pass( String password ) { switch (state) { case START: { giveWarningOfIllegalCommand(); } case HAVE_USER_NAME: { this.password = password; if ( validateUser() ) state = AUTHORIZED; else { sendErrorMessageOrWhatEver(); userName = null; password = null; state = START; } case AUTHORIZED: { endLastSessionWithoutUpdate(); goToStartState() } } } etc. }
Using Polymorphism Implementation
class SPop { private SPopState state = new Start(); public void user( String userName ) { state = state.user( userName ); } public void pass( String password ) { state = state.pass( password ); } public void list( int messageNumber ) { state = state.list( massageNumber ); } etc.
SPopStates Defines default behavior
abstract class SPopState { public SPopState user( String userName ) { return goToStartState(); } public SPopState pass( String password ) { return goToStartState(); } public SPopState list( int massageNumber ) { return goToStartState(); } public SPopState retr( int massageNumber ) { return goToStartState(); } public SPopState quit( ) { return goToStartState(); } protected SPopState goToStartState() { endLastSessionWithoutUpdate(); return new StartState(); } }
SpopStates - Continued
class Start extends SPopState { public SPopState user( String userName ) { return new HaveUserName( userName ); } } class HaveUserName extends SPopState { String userName; public HaveUserName( String userName ) { this.userName = userName; } public SPopState pass( String password ) { if ( validateUser( userName, password ) return new Authorized( userName ); else return goToStartState(); } }
State Intent
Allow an object to alter its behavior when its internal state changes. The object will appear to change it class.
Applicability
Use the State pattern in either of the following cases:
Issues
How much State in the State
In Example:
Issue Who defines the state transitions?
The Context
class SPop { private SPopState state = new Start(); public void user( String userName ) { state.user( userName ); state = new HaveUserName( userName ); } public void pass( String password ) { if ( state.pass( password ) ) state = new Authorized( ); else state = new Start(); }
Who defines the state transitions?
The State
class SPop { private SPopState state = new Start(); public void user( String userName ) { state = state.user( userName ); } public void pass( String password ) { state = state.pass( password ); } public void list( int messageNumber ) { state = state.list( massageNumber ); }
Issue Sharing State Objects
Multiple contexts (SPops) can use the same state object if the state object has no instance variables
A state object can have no instance variables if:
Storing Instance Variables Elsewhere
Variant 1
SPop stores them and passes them to states
class SPop { private SPopState state = new Start(); String userName; String password; public void user( String newName ) { this.userName = newName; state.user( newName ); } public void pass( String password ) { state.pass( userName , password ); }
Storing Instance Variables Elsewhere
Variant 2
SPop stores them and states get data from SPop
class SPop { private SPopState state = new Start(); String userName; String password; public String userName() { return userName; } public String password() { return password; } public void user( String newName ) { this.userName = newName ; state.user( this ); } etc.
class HaveUserName extends SPopState { public SPopState pass( SPop mailServer ) { String useName = mailServer.userName(); etc. } }
Issue Creating and Destroying State Objects
Options:
Issue Changing the Context Class for Real
Some languages allow an object to change its class
| context | context := Start new. context changeClassTo: HaveUserName. context changeClassTo: Authorized.So why not forget State pattern and use:
Consequences
State Verses Strategy
How to tell the difference
Rate of Change Strategy