Chain of responsibility responsibility chain model (C + + responsibility chain model)

Source: Internet
Author: User

CHAINOF Responsibility Responsibility Chain mode: equivalent to the message mechanism in the Mfc,windows programming window.

In MFC, messages are processed in an upward-handed manner, such as the process of a WM_COMMAND message:
1) The MDI main window (CMDIFRAMEWND) receives the command message WM_COMMAND, its ID bit id_xxx;
2) The MDI main window passes the message to the currently active MDI child window (CMDIChildWnd);
3) An MDI child window gives its own child window (view) A processing opportunity to give the message to view;
4) View check own message Map;
5) If the view does not find a program that processes the message, it passes the message to its corresponding Document object, otherwise view processing ends with the message flow.
6) document checks its own message Map, if there is no handler for the message, the message is passed to the documenttemplate processing of its object, otherwise, the message flow is finished;
7) If the message is not processed in 6, the message is returned to view;
8) The view is then passed back to the MDI child window;
9) The MDI child window passes the message to the CWinApp object, and CWinApp provides processing for all non-master messages.

MFC provides a chain-handling strategy for processing messages, and requests to process messages follow a predefined path
Processed at the same time. The sender of the message does not know that the message was finally handled by that particular object, and of course it does not need to be known, but the structure is that the message was processed by an object, or until one of the ultimate objects was processed.
Chain of responsibility Mode description in fact it is such a problem that the object that can process a request is linked into a chain and the request is passed on the chain until an object is processed by the request (it may be necessary to provide a class that handles all requests by default, such as the CWinApp Class in MFC).

Author:jeson yang//date:2014.12.9// filename:main.cpp//////////////////////////////////////////////////////////////////////////#include < Iostream>using namespace Std;class handles{public:virtual ~handles () {}virtual void handlesrequest () =0;void Setsussessor (handles* succ) {m_succ=succ;}; Handles* Getsussessor () {return m_succ;} Protected:handles () {m_succ=0;} Handles (handles* succ) {m_succ=succ;} Private:handles* m_succ;}; Class Concretehandlea:public Handles{public:concretehandlea () {};~concretehandlea () {}; Concretehandlea (handles* succ): Handles (succ) {}void handlesrequest () {if (This->getsussessor ()!=0) {cout<< " To Next SUCC "<<endl;this->getsussessor ()->handlesrequest ();} else{cout<< "A next no More,let do it myself" <<endl;}}}; Class Concretehandleb:public Handles{public:concretehandleb () {};~concretehandleb () {}; Concretehandleb (handles* succ): Handles (succ) {}void handlesrequest () {if (this-> Getsussessor ()!=0) {cout<< "to Next Succ" <<endl;this->getsussessor ()->handlesrequest ();} else{cout<< "B next no More,let do it myself" <<endl;}}}; void Main () {handles* h1=new Concretehandlea (); handles* h2=new Concretehandleb (); H1->setsussessor (H2); H1->handlesrequest ();d elete h2;delete H1;}


Chain of responsibility responsibility chain model (C + + responsibility chain model)

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.