Design Mode (20) --- status Mode

Source: Internet
Author: User

Design Mode (20) --- status Mode

Definition: Allows an object to change its behavior when its internal state changes. This object looks like it has changed its class.

Normal Mode


State: Abstract state role
--- "Interface or abstract class, which defines the object state and encapsulates the environment role to implement status switching.
ConcreteState: Role of a specific State
--- | Each specific State must fulfill two responsibilities: behavior management of the State and process the State. In other words, this state is the thing to do,
And how to transition from this status to another status.
Context Environment role
--- | Defines the interfaces required by the client and switches the status.

The status class implements inter-installation conversion through context (Environment role.

Public class StateTest {public static void main (String [] args) {// provides an environment role Context context = new Context (); // set the handle1 method context for execution status 1. setCurrentState (new ConcreteState1 (); // execute the handle2 method context. handle2 () ;}/ *** abstract State role: * encapsulate the environment role to switch the State * @ author Administrator */abstract class State {// provide an environment role, all sub-classes access protected Context context; public void setContext (Context context) {this. context = context;} // image extraction behavior 1 role pu Blic abstract void handle1 (); // image extraction behavior 2 role public abstract void handle2 ();} /*** switch the specific state * @ author Administrator **/class Context {// it encapsulates all the state methods protected final static ConcreteState1 state1 = new ConcreteState1 (); protected final static ConcreteState2 state2 = new ConcreteState2 (); // set the current State private State currentState; public State getCurrentState () {return currentState;} public void setCurrentState (State CurrentState) {this. currentState = currentState; // sets the environment role this. currentState. setContext (this);} // provides the handle1 method public void handle1 () {this. currentState. handle1 ();} // provides the handle2 method public void handle2 () {this. currentState. handle2 () ;}}/*** implementation class of the specific state class. * The specific implementation method of the main abstract method. * @ Author Administrator */class ConcreteState1 extends State {@ Overridepublic void handle1 () {System. out. println ("I am implementing class 1, the specific implementation method 1... ") ;}@ Overridepublic void handle2 () {System. out. println ("I am implementing class 1, the specific implementation method 2... "); // switch to the handle2 method of the ConcreteState2 class, which is equivalent to switching super. context. setCurrentState (Context. state2); super. context. handle2 () ;}}/*** implementation class of the specific state class. * The specific implementation method of the main abstract method. * @ Author Administrator */class ConcreteState2 extends State {@ Overridepublic void handle1 () {System. out. println ("I am implementing Class 2, the specific implementation method 1... "); super. context. setCurrentState (Context. state1); super. context. handle1 () ;}@ Overridepublic void handle2 () {System. out. println ("I am implementing Class 2, the specific implementation method 2... ");}}

Example: an elevator is in 4 states, including opening, running, stopping, and closing. There is a close status switching relationship between them. For example:
People take the elevator, open --> close ---> RUN ---> stop. When the elevator is closed, the operation and stop may exist. We use the program
To simulate the transition between States.




Public class StateT {public static void main (String [] args) {// provides an environment role LiftContext context = new LiftContext (); // sets the environment role to enable the context. setCurrentLift (new Closeing (); context. close (); context. run (); context. stop () ;}/ *** abstract filling role * defines the image extraction method and encapsulates the role. * @ Author Administrator */abstract class ILift {// defines an environment role. A subclass calls protected LiftContext context; public LiftContext getContext () {return this. context;} public void setContext (LiftContext context) {this. context = context;} // open state of the elevator public abstract void open (); // open and close public abstract void close (); // open and run public abstract void run (); // open and stop public abstract void stop ();} /*** implementation class of the abstract state class * @ author Administrator */class Openning extends ILift {@ Overridepublic void open () {System. out. println ("--- open the elevator. Please go up... or ");} @ Overridepublic void close () {// status conversion super. context. setCurrentLift (LiftContext. CLOSE); // Delegate to the closed class to execute super. context. close () ;}@ Overridepublic void run () {System. out. println ("open state -- cannot run .. ") ;}@ Overridepublic void stop () {System. out. println ("open status -- stopped") ;}/ *** abstract status class implementation class * @ author Administrator */class Closeing extends ILift {@ Overridepublic void open () {// switch to the enabled super. context. setCurrentLift (LiftContext. OPEN); super. getContext (). open () ;}@ Overridepublic void close () {System. out. println ("elevator door closed .. ") ;}@ Overridepublic void run () {super. context. setCurrentLift (LiftContext. RUN); super. getContext (). run () ;}@ Overridepublic void stop () {}}/*** implementation class of the abstract state class * @ author Administrator */class Running extends ILift {@ Overridepublic void open () {System. out. println ("running -- the elevator cannot be opened .. ") ;}@ Overridepublic void close () {System. out. println ("running -- the elevator is closed .. ") ;}@ Overridepublic void run () {System. out. println ("--- the elevator is running... ") ;}@ Overridepublic void stop () {// running status ---> stop status super. context. setCurrentLift (LiftContext. STOP); super. getContext (). stop () ;}/ *** implementation class of the abstract state class * @ author Administrator */class Stoping extends ILift {@ Overridepublic void open () {// after stopping --> open elevator door super. context. setCurrentLift (LiftContext. OPEN); super. getContext (). open () ;}@ Overridepublic void close () {System. out. println ("stopped -- the elevator is still closed .. ") ;}@ Overridepublic void run () {System. out. println ("stopped -- the elevator is not running .. ") ;}@ Overridepublic void stop () {System. out. println ("-- the elevator stops .. ") ;}}/*** environment role, * used to convert the states between them * @ author Administrator */class LiftContext {// encapsulate four state classes protected static final Openning OPEN = new Openning (); protected static final Closeing CLOSE = new Closeing (); protected static final Running RUN = new Running (); protected static final Stoping STOP = new Stoping (); // provide a status class private ILift currentLift; public ILift getCurrentLift () {return currentLift;} public void setCurrentLift (ILift currentLift) {this. currentLift = currentLift; // set the environment role this. currentLift. setContext (this);} // provides four state classes: public void open () {this. currentLift. open () ;}// provides four state classes: public void close () {this. currentLift. close () ;}// provides four state-class Methods: public void run () {this. currentLift. run () ;}// provides four state-class Methods: public void stop () {this. currentLift. stop ();}}
Advantages of status mode:
--- | Clear structure
--- | Follow the design principles
--- | Excellent Encapsulation
Disadvantages of status mode:
--- | There are too many role subclasses encapsulated in Environment roles. Poor management.
Use Cases of status Mode
--- | Scenarios in which behavior changes as the status changes
--- | Substitution of condition and Branch judgment statements.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.