Design pattern C + + implementation (14)--Responsibility chain mode __c++

Source: Internet
Author: User

Design patterns in the software domain provide a useful way for developers to use expert design experience. The design pattern uses the Object-oriented programming language the important characteristic: the encapsulation, the inheritance, the polymorphism, the true understanding design pattern essence is possibly a long process, needs the massive practical experience accumulation. Recently read the design pattern of the book, for each model, with C + + wrote a small example, deepen understanding. The main reference "Dahua design pattern" and "design pattern: Reusable object-oriented Software Foundation" two books. This article introduces the realization of decoration mode.

Responsibility chain pattern: Enables multiple objects to have the opportunity to process requests, thereby avoiding coupling between the sender and receiver of the request. Connect the objects into a chain and pass the request along the chain until an object handles it. The idea is simple, considering employees asking for a raise. The company's managers have a total of three, general manager, director, manager, if an employee demands a raise, should apply to the manager, if the amount of pay in the manager's authority, then the manager can directly approve, otherwise will be submitted to the Director of the application. The director's approach is the same, and the general manager can handle all requests. This is a typical chain of responsibilities, and the processing of requests forms a chain until an object processes the request. A UML diagram of this example is given.


The implementation of the code is relatively simple, as follows:

[CPP]  View plain copy print? Abstract Manager    class manager   {   protected:        manager *m_manager;       string m_name;   public:       manager (manager *manager, string name): M_manager (Manager),  m_name ( Name) {}       virtual void dealwithrequest (string name, int  num)   {}  };  /manager    class commonmanager: public  manager   {   public:       commonmanager (Manager *manager ,  string name): Manager (manager,name)  {}       void  Dealwithrequest (string name, int num)         {           if (num < 500)  //Manager within the purview &Nbsp;          {                cout<< "manager" <<m_name<< "approval" <<name<< "raise" << num<< "Yuan" <<endl<<endl;           }            else            {               cout<< " Manager "<<m_name<<" can not be processed, referred to the director "<<endl;                m_manager->dealwithrequest (name, num);            }       }  };  //director    class majordomo: public manager   {   public:        majordomo (MAnager *manager, string name): Manager (Manager,name)  {}        Void dealwithrequest (string name, int num)         {            if (num < 1000)  //Director's authority             {                cout<< "Director" <<m_name<< "Approval" <<name<< "raise" <<num << "Yuan" <<endl<<endl;           }           else            {               cout<< "Director" <<m_name<< "cannot be processed, referred to general manager" <<endl;               &nbSp;m_manager->dealwithrequest (name, num);           }        }  };  //General manager    Class generalmanager:  public Manager   {   public:  <

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.