Design Mode-state)

Source: Internet
Author: User
Design Mode (20): State)
State)
Definition
When the internal state of an object changes, it is allowed to change its behavior. This object seems to have changed its class.
Motivation
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. 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.
State structure
State class, abstract state class, defines an interface to encapsulate a specific State-related Behavior of the context.
Abstract   Class State
{
Public   Abstract   Void Handle (context );
}
Concretestate class, specific State. Each sub-class implements a state-related behavior related to the context.
Class Concretestate: State
{
Public   Override   Void Handle (context)
{
Context. State =   New Concretestateb (); // Set the next status of concretestatea to concretestateb.
}
}

ClassConcretestateb: State
{
Public Override VoidHandle (context)
{
Context. State= NewConcretestatea ();
}
}

The context class maintains an example of a conctext subclass. This instance defines the current status.
Class Context
{
Private State state;
Public Context (State state)
{
This . State = State;
}
Public State state
{
Get { Return State ;}
Set
{
State = Value;
Console. writeline ( " Current status: "   + State. GetType (). Name );
}
}
Public   Void Request ()
{
State. Handle ( This ); // Process the request and set the next status
}
Client Code
Static   Void Main ( String [] ARGs)
{
Context C =   New Context ( New Concretestatea ());
C. Request ();
C. Request ();
C. Request ();
C. Request ();
}
Advantages of status mode:
The behavior localization related to a specific State and the behavior of different States are separated. Puts specific status-related behaviors into an object. Because all status-related code exists in a concretestate, therefore, new statuses and transformations can be easily added through the new subclass. In this way, a large number of conditional branch statements are eliminated.
The State mode distributes various state transfer logics to sub-classes of the State to reduce their dependencies.

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.