Design mode-state mode C + + implementation

Source: Internet
Author: User
Tags class definition

1 definitions

When the internal state of a state changes to allow its behavior to change, the object looks like it has changed its class

2 class Diagram

Role analysis

State abstract status role, interface, or abstract class, responsible for state definitions, and encapsulating environment roles for state switching

Concretestate The specific status role, complete two duties: the State behavior management and the trend state processing--== This state to do and how this state over to other states

Context environment role, which defines the interfaces required by the client, and is responsible for the switchover of the specific state

Realize

#include <iostream>using namespacestd;//declaration of preceding paragraphclassContext;//abstract State class definitionclassstate{protected: State () {} Public:    Virtual~state () =0    {}; voidSetContext (Context *x) {_context=x; }    Virtual voidHandler1 () =0; Virtual voidHandler2 () =0;Private: Context*_context;};//Specific status DefinitionsclassConcreteState1: Publicstate{ Public: ConcreteState1 () {}~ConcreteState1 () {}voidhandler1 () {cout<<"Concretestate1::handler1"<<Endl; }    voidHandler2 () {cout<<"Concretestate1::handler2"<<Endl; }Private:};//Specific status DefinitionsclassConcreteState2: Publicstate{ Public: ConcreteState2 () {}~ConcreteState2 () {}voidhandler1 () {cout<<"Concretestate2::handler1"<<Endl; }    voidHandler2 () {cout<<"Concretestate2::handler2"<<Endl; }Private:};//Environment Classclasscontext{ Public: Context (): Sta1 (NewConcreteState1), Sta2 (NewConcreteState2) {} State*getcurrentstate () {returnCurrentState; }    voidSetcurrentstate (state*s) {CurrentState=s; CurrentState->setcontext ( This); }    voidhandler1 () {CurrentState-handler1 (); }    voidHandler2 () {CurrentState-Handler2 (); }    ~Context () {}Private:    //The current state is one of all StatesState *CurrentState; //all States should be defined as static more appropriateState *Sta1; State*Sta2;};
3 Applications

① Advantages

State mode hides the changing process of the state, and his switching causes a change in behavior. We see only behavioral changes, not state changes.

Clear structure to avoid the use of too many switch or if statements

Embodies the open and closed principle and single duty principle, each state is a sub-class

Good encapsulation, state changes in the internal embodiment, outside calls do not know this

② Disadvantages

More subclasses can cause problems with class bloat

4 Usage Scenarios

Behavior changes as the state changes

The substitute of the judgment statement of the condition divided by

5 Precautions

State mode is useful when an object changes in his state, and his behavior changes, that is, when the behavior is constrained by the state, using the state pattern, and using the object with no more than 5 states

6 Best Practices

TCP Three state toggle: Wait state, connection status, disconnect state.

Workflow development: State machine Management.

Design mode-state mode C + + implementation

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.