Design Pattern _ Status Pattern

Source: Internet
Author: User

Design Pattern _ Status Pattern
State Pattern

Allow an object to alter its behavior when its internal state changes. the object will appear to change its class. (when an object's internal state changes, it is allowed to change its behavior. This object looks like it has changed its class)

 

It should be said that the builder mode is executed in the Order provided by the customer, so it is designed to cope with a variety of complex orders, and the scalability is similar. The combination of the two will play a very good encapsulation effect. Builder mode adaptation

 

Status mode adaptation

 

ExampleThe elevator can be in four states: Close, open, run, and stop. It cannot run when the door is opened. It cannot open when running ....... the first thought is to take four variables for judgment, which is too ugly.
State class Abstraction
public abstract class LiftState { protected Context context;  public void setContext(Context _context){  this.context=_context; }  public abstract void open(); public abstract void close(); public abstract void run(); public abstract void stop();}
Off status

 

 

public class ClosingState extends LiftState { @Override public void close() {  System.out.println(close the lift); } @Override public void open() {  super.context.setLiftState(Context.openningState);  super.context.getLiftState().open(); } @Override public void run() {  super.context.setLiftState(Context.runningState);  super.context.getLiftState().run(); } @Override public void stop() {  super.context.setLiftState(Context.stoppingState);  super.context.getLiftState().stop(); }}
Stopped
public class StoppingState extends LiftState { @Override public void close() {  //do nothing } @Override public void open() {  super.context.setLiftState(Context.openningState);  super.context.getLiftState().open(); } @Override public void run() {  super.context.setLiftState(Context.runningState);  super.context.getLiftState().run(); } @Override public void stop() {  System.out.println(the lift is stoped); }}
Running status
public class RunningState extends LiftState { @Override public void close() {  //do nothing } @Override public void open() {  //do nothing } @Override public void run() {  System.out.println(lift is runing...); } @Override public void stop() {  super.context.setLiftState(Context.stoppingState);  super.context.getLiftState().stop(); }}
Opening status
Public class OpenningState extends LiftState {@ Override public void close () {super. context. setLiftState (Context. closingState); // call the method super in Context. context. getLiftState (). close (); // call another state method through Context} @ Override public void open () {System. out. println (lift is open ...);} @ Override public void run () {// do nothing} @ Override public void stop () {// do nothing }}
Context object
Public class Context {public final static OpenningState openningState = new OpenningState (); public final static ClosingState closingState = new ClosingState (); public final static RunningState runningState = new RunningState (); public final static StoppingState stoppingState = new StoppingState (); private LiftState liftState; public LiftState getLiftState () {return liftState;} public void setLiftState (LiftState liftState) {this. liftState = liftState; this. liftState. setContext (this); // set the context object} public void close () {this. liftState. close ();} public void open () {this. liftState. open ();} public void run () {this. liftState. run ();} public void stop () {this. liftState. stop ();}}

Scenario Test

 

 

Public class Client {public static void main (String [] args) {Context context = new Context (); context. setLiftState (Context. closingState); context. open (); context. close (); context. run (); context. stop () ;}/ * Context object Context, member attribute LiftState. Four states can be set to get and change the state, which is equivalent to an abstract LiftState of four States of a public container, the member attribute Context allows you to set Context objects so that all the four States have the same Context. You have one of them, and you can switch the State through the container Context, when the state changes, the LiftState state is changed through the Unified Context of the parent class, and then the changed state method is called, that is */

Result

 

 

lift is open...close the liftlift is runing...the lift is stoped

 

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.