I. Definition of state mode: State mode is the main solution when the conditional expression that controls an object state is too complex. It is possible to simplify the complex judgment logic by transferring the judgment logic of State to a series of classes representing different states.
Two. Intent of the state mode: Allows an object to change its behavior when its internal state changes.
Three. Application scenarios for State mode:
1. The behavior of an object depends on its state, and it must change its behavior according to the state at run time.
2. An operation contains a large, multi-branched structure, and these branches are determined by the state of the object.
Four. Implementation of the state mode:
<summary>/// define interfaces to encapsulate the behavior of a context object in a specific state/// </summary> public abstract class Abstractstate {public abstract void handle (context context); }
<summary>/// The behavior of specific a state//// </summary> public class Concretestatea:abstractstate {public override void handle (context context) { Console.WriteLine ("Current state: {0}", this.) GetType (). ToString ()); Change the state of the context. state = new Contextstateb (); } }
<summary>///specific behavior of B/C/// </summary> public class Contextstateb:abstractstate {public override void handle (context context) { Console.WriteLine ("Current state: {0}", this.) GetType (). ToString ()); Change the status to C context. state = new Contextstatec (); } }
<summary>////specific C-state for behavior/// </summary> public class Contextstatec:abstractstate {public override void handle (context context) { Console.WriteLine ("Current state: {0}", this.) GetType (). ToString ()); Change the status to a context. state = new Concretestatea (); } }
The public class Context { //context class needs to maintain an instance of Concretestate, which gives an example of the existing state of an object in this environment. Public abstractstate State {get; set;} Public Context (abstractstate state) {this . state = state; } public void Request () { state.handle (this); } }
<summary> //C # design mode-state mode/ //</summary> class program { static void Main (string [] args) { context c = new Context (new Concretestatea ()); C.request ();//In Concretestatea, I switch the state of the context back to Contextstateb, c.request ();//And CONTEXTSTATEB, I switch the state of the context back to Contextstatec, c.request ();//And Contextstatec, I switch the state of the Context back to Concretestatea, c.request ();//This enables The Context loops in the three states of A, B, C. } }
Five. Summary:
In some cases, the behavior of an object depends on one or more dynamically changing properties, which are called states, the objects that have these properties are called State objects, and the states of those objects are taken out of a predefined set of values, and when an object interacts with an external event, The internal state of the system changes, so that the behavior of the systems changes accordingly. State mode wraps the behavior of the object being studied in different state objects,
Each state object belongs to a subclass of an abstract state class, and the intent of the state pattern is to change the behavior of an object as it changes its internal state.
C # design mode-state mode