Design mode-the state mode (with code)

Source: Internet
Author: User

The state pattern is that when an object's internal state changes to allow its behavior to change, the object looks like it has changed the class.

Main uses:
State mode mainly solves the situation when the expression that controls an object's state transformation is too complex. Transfer the state's judgment logic to some column classes that represent different states.
:


The essence of this diagram is the transition between States, for us to simulate a situation, context is an environment, we according to the current state, to do different things for the environment, if the current environment does not meet our state, we can also change.

If we do not use this state pattern, then the consequence is that in a method there is a large number of if,else judgments, too long method is enough to arouse our attention.

A particular state-related behavior is put into an object, because all state-related code exists in the touch of a concretestate, so adding a new state we can add the class.
The state mode reduces interdependence by distributing the various state transfer logic to the sub-classes of States.
The benefit of a state pattern is to localize the language associated with a particular state and separate the behavior of the different states.

Let's look at the code I wrote:

#include <iostream>using namespace Std;class context;class abstractstate;class concretestateb;class Concretestatea;class abstractstate{public:virtual void Handle (context & context) = 0;}; Class Context{public:abstractstate *state;int hour; Context (abstractstate *state): state {}virtual void request () {state->handle (*this);}}; Class concretestateb:public abstractstate{public:virtual void handle (Context & context) {if (Context.hour >= 12) { cout << "It's over 12, what else can I do" << Endl;}}; Class concretestatea:public abstractstate{public:virtual void handle (Context & context) {if (Context.hour <) {C Out << "Not yet 12, what to Do" << Endl;} Else{context.state = new Concretestateb;context.request ();}}; void Main () {abstractstate *pstatea = new Concretestatea; Abstractstate *pstateb = new Concretestateb; Context *pcontext = new Context (Pstatea);p context->hour = 9;pcontext->request ();p context->state = Pstateb; Pcontext->hour = 15;pcontext->request ();d ELete Pstatea;delete pstateb;cin.get ();} 

We do not care about memory leaks, here is, our state class can access the internal members of the context, according to his members and our status to do the appropriate operation, that is, if one day we need to add state or minus state, we can directly delete or add the class just fine, if it is, Else's structure, it violates the principle of openness, closure, and a single duty.

Well, Bo mainly sleep, good sleepy, goodnight ~

Design mode-the state mode (with code)

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.