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: <