State mode: Wraps the executed action in a class related to the state, which determines how the action should be executed by the state.

Source: Internet
Author: User

Once discussed with colleagues how to program the elevator control system, the result is that if the structure of the program to write elevators, the system must be very complex and huge. Then I wrote a simple elevator control program with state mode, and the result was greatly simplified. Here is the architecture of a simple single elevator control program that I have written:



State mode is a little more complicated, first of all, we analyze an elevator can perform actions: closing, opening, upward, downward, stop. So we define a elevatorstate interface with the Closedoor (),Opendoor () and other five kinds of actions that can be performed by elevators.

Then we analyzed that a single elevator should have a variety of states, including: door closed, door open, upstream, downstream, stopped. So we have defined five implementationselevatorstateThe class of the interface,doorclosestate、dooropenstate、movingupstate、movingdownstate、stopstate, respectively, corresponding to the five states of the elevator. Each of the five state classes includes aElevatorType of member, that is, the class of the elevator.

Elevator class Elevator has a member representing five states, all of which are elevatorstate interface types. The constructor of the elevator class creates the corresponding implementation class for the members representing the five states and assigns values, as follows:

Publicelevator () {

stopstate= New Stopstate (this);

movingupstate= New Movingupstate (this);

movingdownstate= New Movingdownstate (this);

dooropenstate= New Dooropenstate (this);

doorclosestate= New Doorclosestate (this);



State= stopstate;

}

elevator class also has a call method, on behalf of the user calls the elevator action, call the elevator current state of closing, uplink or downlink, stop, open door and other actions, as follows:

Publicvoid call (int destfloor) {

if (Destfloor! = Currentfloor) {

State.closedoor ();

if (Destfloor > Currentfloor) {

State.moveup (Destfloor);

}else if (Destfloor < Currentfloor) {

State.movedown (Destfloor);

}

State.stop ();

State.opendoor ();

}else {

System.out.println ("Elevatoris at" + Destfloor + "floor Now");

}



}

It can be seen that the elevator can perform the current action, how to execute the logic is encapsulated in the elevator state class, so the logic of the judgment is relatively simple, do not consider the situation under a variety of conditions, only consider how to deal with the current state, for example, we look at the elevator upstream state how to handle the door action:

< Span style= "Font-family:monospace" > public Span style= "COLOR: #000000" > void opendoor () {

System. out . println ("Can ' Topen the door during move." );

}

It is not necessary to make any logical judgment at this time, tell the user directly: "Cannot open the door in the elevator movement".

State mode is related to the state of the action that the object can perform, and there are many situations where the state mode will greatly simplify the judgment of logic, make the code clear, easy to confuse and error.


State mode: Wraps the executed action in a class related to the state, which determines how the action should be executed by the state.

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.