Design Pattern learning-state pattern

Source: Internet
Author: User

I. State mode: State mode. When an object's internal state changes, its behavior can be changed. This object changes its subclass. The State mode mainly solves the problem when the conditional expressions that control the state transition of an object are too complex. Transferring the status judgment logic to a series of classes that indicate different states can simplify the complicated judgment logic. Of course, if this state is relatively simple, there is no need to use the "State mode. This section is a bit difficult to understand. In short, in a class, the next state subclass is instantiated based on the current state. The following figure shows the status mode structure. The State class is an abstract State class that defines an interface to encapsulate behaviors related to the specific State of the Context. 1 public abstract class State2 {3 public abstract void Handl (Context context); 4} ConcreateState class, which is a specific State class that inherits the State class, each subclass implements a state-related Behavior of Context. Copy code 1 public class ConcreateStateA: State 2 {3 public void Handl (Context context) 4 {5 // set the next State ConcreateStateB 6 context of ConcreateStateA. state = new ConcreateStateB (); 7} 8} 9 10 public class ConcreateStateB: State11 {12 public void Handl (Context context) 13 {14 // set ConcreateStateB's next State ConcreateStateA15 context. state = new ConcreateStateA (); 16} 17} copy the Context class of the Code to maintain an instance of the ConcreateState subclass and define the current State in the instance. Status. Copy code 1 public class Context 2 {3 private State state; 4 public Context (State state State) 5 {6 // initialize the initial state of Context in the constructor. 7 state = state); 8} 9 // read/write status attribute, used to read the current State and set the new state 10 public state State11 {12 get13 {14 return this. state; 15} 16 set17 {18 this. state = value; 19 Conole. writeLine ("Current status: {0}", state. getType (). name); 20} 21} 22 23 public void Request () 24 {25 // process the Request and set the next status to 26 States. handle (this); 27} 28 29} copy the code client call code: Copy code 1 static void main (string [] args) 2 {3 4 Context context = new Context (new ConcreateStateA (); // set Co The ntext initialization status is ConcreateState 5 context. request (); 6 context. request (); 7 context. requset (); 8 context. requset (); 9 10 Console. readKey (); 11} copy the Code [execution result diagram] 2. Advantages of the state mode and the use state mode are the behavior localization related to the specific state, in addition, the behavior of different States is separated to eliminate large conditional branch statements. Large branch judgments make it difficult for them to modify and expand, any changes or changes may cause fatal errors in the program. The State mode distributes various State transfer logics to sub-classes of the State to reduce their dependencies. When will the status mode be used? When an object's behavior depends on its state, and it must change its behavior according to its state at runtime, you can consider using the state mode. In addition, if the business requires that a business has multiple States, usually some enumeration constants, the state changes are implemented by a large number of multi-branch judgment statements, in this case, we should consider defining each business State as a subclass of State, so that these objects can change independently without other objects. III. On the way to work-state mode first, let's take a look at the code structure diagram to abstract the WaitingBus class. A WaitBus method is defined and an OnRoad object parameter is required. 1 public abstract class WaitingBus2 {3 public abstract void WaitBus (OnRoad road); 4} status class of buses such as departing from home in the morning. Copy the Code 1 // <summary> 2 // leave home, wait for the bus on the platform 3 /// </summary> 4 public class LeaveHome: waitingBus 5 {6 public override void WaitBus (OnRoad road) 7 {8 if (road. busCome = "not yet entered") 9 {10 Console. writeLine ("Current status: {0}, anxious, coming late for work. ", Road. busCome); 11} 12 else13 {14 road. waiteBus = new BusComing (); 15 road. road (); 16 17} 18} 19} copy the status class of the bus to prepare for the bus arrival. Copy the Code 1 // <summary> 2 // The car is preparing to enter the station. Wait for the bus to arrive. 3 /// </summary> 4 public class BusComing: waitingBus 5 {6 public override void WaitBus (OnRoad road) 7 {8 if (road. busCome = "prepare for inbound") 9 {10 Console. writeLine ("Current status: {0}, waiting to get on the bus, excited, cannot be late at work. ", Road. busCome); 11} 12 else13 {14 road. waiteBus = new BusComedGetOn (); // transfer to next status 15 road. road (); // process the next State request. 16} 17} 18} copy the code to the bus to enter the station. The bus is in the status class. Copy the Code 1 // <summary> 2 // bus stops and starts to get on the bus 3 /// </summary> 4 public class BusComedGetOn: waitingBus 5 {6 public override void WaitBus (OnRoad road) 7 {8 if (road. busCome = "get on") 9 {10 Console. writeLine ("Current status: {0}, get on the bus, hurry to find a seat that nobody is in, feel better. ", Road. busCome); 11} 12 else13 {14 road. waiteBus = new OnRoading (); 15 road. road (); 16} 17} 18} copy the status class of the Code bus on the way. Copy the Code 1 /// <summary> 2 // when the bus is on its way to the company, 3 /// </summary> 4 public class OnRoading: waitingBus 5 {6 public override void WaitBus (OnRoad road) 7 {8 if (road. busCome = "running") 9 {10 Console. writeLine ("Current status: {0}. Check the news online on your mobile phone. Hey, there's an old lady standing next to it. Let's get your seat right away. ", Road. busCome); 11} 12 else13 {14 road. waiteBus = new Arrival (); 15 road. road (); 16} 17} 18} status class where the code is copied to the destination. Copy the Code 1 // <summary> 2 // get to the bus and get off to the Company 3 /// </summary> 4 public class Arrival: waitingBus 5 {6 public override void WaitBus (OnRoad road) 7 {8 Console. writeLine ("Current status: {0}, finally arrived, get off to the company. It took another 10 minutes to breathe a sigh of relief. ", Road. busCome); 9} 10} copy the code client call code: Copy code 1 class Program 2 {3 static void Main (string [] args) 4 {5 OnRoad road = new OnRoad (new LeaveHome (); 6 road. busCome = "not yet available"; 7 road. road (); 8 road. busCome = "prepare for arrival"; 9 road. road (); 10 road. busCome = "get on the bus"; 11 road. road (); 12 road. busCome = "Driving"; 13 road. road (); 14 road. busCome = ""; 15 road. road (); 16 17 Console. readKey (); 18 19} 20}

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.