Select Page

State

State

Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

Source Code

/**  * Defines an interface for encapsulating the behavior associated with a  * particular state of the Context.  *   * @role __State  */ public interface State {         void handle(String sample);}  /**  * Defines an interface of interest to clients. Maintains an instance of a  * ConcreteState subclass that defines the current state.  */ public class Context {         private State state;        public void setState(State newState) {                 this.state = newState;        }          public void someOperation() {                 state.handle("aaa");        }  }  /**  * Implements a behavior associated with a state of the Context.  */ public class ConcreteState implements State {         public void handle(String sample) {                 /* put your code for this particular state here */         }  }  /pre>
0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments

Categories

0
Would love your thoughts, please comment.x
()
x