C # Design Pattern Series Tutorials-state mode _c# Tutorial

Source: Internet
Author: User

1. Overview

When an object's internal state changes allowing it to change its behavior, the object looks like it has changed its class.

2. The problem addressed

The main solution is when the conditional expression that controls an object state transition is too complex. It is possible to simplify the complex logic judgment by transferring the judgment logic of the state to the different series of classes.

3. Role in the pattern

3.1 Context: It defines the interfaces required by the client program and maintains an instance of the specific status role, which is delegated to the current concrete state object for processing.

3.2 Abstract state: Defines an interface to encapsulate behavior associated with a particular state that uses a context environment.

3.3 Specific state: an interface that implements an abstract state definition concrete.

4. Pattern Interpretation

4.1 Class diagram of State mode

4.2 Code implementation of State mode

 <summary>///Context class, which maintains an instance of the Concretestate subclass that defines the current state.
  </summary> public class Context {private state State; <summary>///defines the initial state of the context///</summary>///<param name= ' state ' ></param> public Co
  ntext (state state) {this.state = state; ///<summary>///State Properties for reading and setting new states///</summary> public state ' {get ';
  set {state = value;} ///<summary>///handles the request and sets the next status///</summary> public void Request () {state.
  Handle (this);
  }///<summary>///Abstract State class, defines an interface to encapsulate the behavior associated with a particular state of the context///</summary> public abstract class States {
 public abstract void Handle (context context);
 ///<summary>///specific state class, each subclass implements a behavior related to a state of the context///</summary> public class Concretestatea:state {///<summary>///setting the next state of Concretestatea is Concretestateb///</summary>///<param name= "contExt "></param> public override void Handle (context context) {Console.WriteLine (" Current state is a. "); Context.
  state = new Concretestateb (); } The public class Concretestateb:state {///<summary>///settings Concretestateb the next state is Concretesatea///</ summary>///<param name= ' context ' ></param> public override void Handle (context context) {Console .
   WriteLine ("Current state is B."); Context.
  state = new Concretestatea ();

 }
 }

4.3 Client invocation

 Class program
 {
  static void Main (string[] args)
  {
   //Set the initial state of the context to the Concretestatea
   context Context = new Context (new Concretestatea ());

   Keep making requests while changing the state context
   . Request ();
   Context. Request ();
   Context. Request ();
   Context. Request ();

   Console.read ();
  }
 

Run results

5. Model Summary

5.1 Advantages

The 5.1.1 state pattern will localize the behavior associated with a particular state and separate the behavior of different states.

5.1.2 All state-related code exists in a conceretestate, so it is easy to add new states and transformations by defining new subclasses.

The 5.1.3 state pattern reduces dependencies between states by not splitting the various state-transition logic into the subclass of the class.

5.2 Disadvantages

5.2.1 leads to more concretestate subclasses

5.3 Applicable Scenarios

5.3.1 when an object's behavior depends on its state, and it must change its behavior according to its state at run time, consider using the state mode.

5.3. The 21 operations contain a large branching structure, and these branches are determined by the state of the object.

6. Application Example: The electric light has two states, on (on) and off (not bright), the following state mode to achieve the control of the lamp.

6.1 Class Diagram

6.2 Implementation Code

 <summary>///Lamp category, the context class in the corresponding mode///</summary> public class Light {private lightstate state;
  Public Light (lightstate state) {this.state = state; ///<summary>///Press the light switch///</summary> public void Pressswich () {state.
  Pressswich (this);
   Public lightstate state {get}
  set {state = value;} }///<summary>///abstract Light status class, equivalent to state class///</summary> public abstract class Lightstate {public AB
 stract void Pressswich (Light Light); ///<summary>///Specific status class, open///</summary> public class On:lightstate {///<summary>///in
  In the open state, press the switch to switch to the off state.
   </summary>///<param name= "Light" ></param> public override void Pressswich (light light) {

   Console.WriteLine ("Turn Off the Light"); Light.
  state = new Off (); }///<summary>///specific status class, off///</summary> public class Off:lightstate {///<summary&Gt
  In the closed state, press the switch to turn on the light.
   </summary>///<param name= "Light" ></param> public override void Pressswich (light light) {

   Console.WriteLine ("Turn on the Light"); Light.
  state = new On ();

 }
 }

6.3 Client Code


 Class program
 {
  static void Main (string[] args)
  {
   //Initialize light, original state is off
   Light Light = new Light (new Off ()) ;

   Press the switch for the first time and turn on the light
   light. Pressswich ();
   The second time press the switch, turn off the light
   light. Pressswich ();

   Console.read ();
  }
 

Execution results

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.