Design mode State-- Transportation (original)

xiaoxiao2021-03-06  74

State of design pattern - transportation

"Children crossing the road, left, red light stop, green light, Auntie is a baby." In order to give her clear, I specially to take her to see the running of the traffic light near our home. We all know that there are three status of traffic lights: redlights, yellowlights (Yellowlight) and green lights (Greenlight). The change in traffic lights is controlled by the control center (ControlCenter).

Let's take a look at how this process is implemented.

1, here, first define traffic lights (TrafficLight) interface class:

Public interface trafficLight {

Public void showredLight (); // Show red light

Public void showgreenlight (); // Show green light

Public void showyellowlight (); // Show yellow light

}

2, the traffic light near our home is the specific implementation of the TrafficLight interface of the Traffic Light: TRAFFICLIGHT:

Public class nearmyFamilyTraffiTriGHT IMPLEments TrafficLight {

Public void showredLight () {

System.out.println ("Red Light is on, you can't pass!");

}

Public void showgreenlight () {

System.out.println ("green light is bright, you can pass!");

}

Public void showyellowlight () {

System.out.println ("yellow light is bright!");

}

}

3. Define the Control Center class:

Public class controlcenter {

Private nearmyFamilyTraffiGHT TrafficLight; // Traffic lights near our home

Public void changestate (nearmyFamilyTraffiGht TrafficLight) {

THIS.TRAFFICLIGHT = TrafficLight;

}

Public void showredLight () {// Show red light

TrafficLight.showRedlight ();

}

Public void showgreenlight () {// Show green light

TrafficLight.showgreenlight ();

}

Public void showyellowlight () {// Show yellow light

TrafficLight.Showyellowlight ();

}

}

4, the traffic light near our home (NearmyFamilyTraffiTe is actually red, yellow, green three lamps:

A: Red Light (Redlight) class:

Public Class Redlight Extends NearmyFamilyTrafficLight {

Public static boolean existredLight = false;

Public static redlight getredlight () {// Get red light IF (EXISTREDLIGHT == false) {

EXISTREDLIGHT = TRUE;

Return new redlight ();

}

Return NULL;

}

}

B: Greenlight class:

Public class greenlight extends nearmyfamilytrafficLight {

Public static boolean existgreenlight = false;

Public static greenlight () {// Get green light

IF (existgreenlight == false) {

EXISTGREENLIGHT = TRUE;

Return new greenlight ();

}

Return NULL;

}

}

C: Yellow Light (Yellowlight) class:

Public Class Yellowlight Extends NearmyFamilyTraffiGHT {

Public static boolean existYellowlight = false;

Public Static Yellowlight getyellowlight () {// get yellow light

IF (existyellowlight == false) {

EXISTYELLOWLIGHT = TRUE;

Return New Yellowlight ();

}

Return NULL;

}

}

5, write test classes:

Public class test {

Public static void main (string args []) {

ControlCenter ControlCenter = New ControlCenter (); // Control Center

NearMyFamilyTrafficLight redLight = RedLight.getRedLight (); // Red NearMyFamilyTrafficLight greenLight = GreenLight.getGreenLight (); // green NearMyFamilyTrafficLight yellowLight = YellowLight.getYellowLight (); // yellow

ControlCenter.ChangeState (redlight); // change to red light status

ControlCenter.ShowRedlight (); // Show red light

ControlCenter.changeState (YELLOWLIGHT); / / Change into yellow light status

ControlCenter.Showyellowlight (); // Show yellow light

ControlCenter.changeState (Greenlight); // Change to green light status

ControlCenter.ShowGreenlight (); // Show green light

}

}

6. Description:

A: State definition: Different states, different behaviors; or, each state has the corresponding behavior.

B: We can see that the change in the state is a control center to control, and the normal operation of the traffic is implemented by displaying different lights.

C: Therefore, when there is a status to switch this matter, we can use State this mode.

转载请注明原文地址:https://www.9cbs.com/read-107074.html

New Post(0)