[Design mode series] Status mode of behavior mode

Source: Internet
Author: User

Summary
I was not prepared to write the Status mode, because it is similar to the Strategy Mode, similar to the double brother, similar to the class structure, similar to the processing method, almost nothing to say, and then I had to weigh it down, after all, this is also a mode independently proposed by GOF, and the Status mode and Stragegy mode have some different concerns. So what can the Status mode do? How can this problem be solved?
Various States are often involved in programs. Each state has different processing logic, and the status can be switched. Changing the status also requires changing their behavior, in this case, our program can easily fall into various coupling traps. The Status mode can help us solve such problems and make the program easy to expand and clear. In fact, the Strategy Mode encapsulates inter-algorithm switching, while the Status mode implements inter-state switching. The focus is different, but the idea is the same. The Strategy mode can be found below:
[Design pattern series] Strategy pattern of behavioral pattern

Purpose
Encapsulate various States independently, and isolate the state switching and corresponding processing, so that the object changes the corresponding state behavior when changing the internal state.

Instance
To develop a project management system, the project goes through various stages (which can be viewed as various States). Here we assume that only four stages are considered: requirement, design, coding, and testing. At different stages, the system can output different project document reports, such as requirement documents, design documents, code review records, and test documents. Check how the Status-based mode is considered.
We encapsulate various stages (States) into different classes. Each state class has a Report method to process different Report content, and these status classes do not care about how to switch the status, how is the behavior changed? these operations will be encapsulated into an independent Context class. The class diagram and Code are as follows:


[Cpp]
Class Status {
Public:
Virtual void Report () = 0;
};
Class RequirementSts: public Status {
Public:
Virtual void Report (){};
};
Class DesignSts: public Status {
Public:
Virtual void Report (){};
};
Class CodingSts: public Status {
Public:
Virtual void Report (){};
};
Class TestingSts: public Status {
Public:
Virtual void Report (){};
};
Class Context {
Public:
Context (Status * sts ){
MSts = sts;
}
Void SetStatus (Status * sts ){
MSts = sts;
}
Void Report (){
MSts-> Report ();
}
Private:
Status * mSts;
};

The Client can use the following code:
[Cpp]
Status * require = new RequirementSts ();
Context * cntxt = new Context (require );
Cntxt-> Report (); // requirement report
Status * design = new DesignSts ();
Context * cntxt = new Context (design );
Cntxt-> Report (); // design report
......

It can be seen that when the Context class status changes, the corresponding behavior will also change automatically. Of course, we can further encapsulate the state change process into the Context class. First, the Context class must contain four State objects, that is, you need to pass a status group in the constructor parameters, and then encapsulate the status switching process to the NextStatus method. The Client can automatically switch the status and behavior by calling the NextStatus method. The Code is as follows:
[Cpp]
Class Context {
Public:
Context (Status ** sts, int stsCount ){
MSts = sts;
MCount = stsCount;
MIndex = 0;
}
Void NextStatus (){
MIndex ++;
If (mIndex> = mCount ){
MIndex = 0;
}
}
Void Report (){
MSts [mIndex]-> Report ();
}
Private:
Status ** mSts;
Int mCount;
Int mIndex;
};
Client call:
[Cpp]
Status * sts [4];
Sts [0] = new RequirementSts ();
Sts [1] = new DesignSts ();
Sts [2] = new CodingSts ();
Sts [3] = new TestingSts ();
Context * cntxt = new Context (sts );
Cntxt-> Report (); // requirement report
Cntxt-> NextStatus ();
Cntxt-> Report (); // design report
Cntxt-> NextStatus ();
Cntxt-> Report (); // coding report
Cntxt-> NextStatus ();
Cntxt-> Report (); // testing report
Author: my_business

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.