The design patterns in the software field provide developers with an effective way to use expert design experience. The design patterns use the important features of object-oriented programming languages: encapsulation, inheritance, and polymorphism. It may be a long process to truly comprehend the essence of the design patterns, which requires a lot of practical experience. I recently read a book on design patterns. I wrote a small example in C ++ for each pattern to help me better understand it. Refer to "big talk Design Patterns" and "design patterns: Reusable basics of object-oriented software. This article describes how to implement the decoration mode.
Responsibility Chain Mode: Enables multiple objects to process requests to avoid coupling between request senders and receivers. Connect these objects into a chain and pass the request along the chain until an object processes it. The idea is simple. Consider asking for a raise. There are three levels of managers in the company: General Manager, Director, and manager. If an employee asks for a salary increase, apply to the manager in charge. If the number of salary increases is within the Manager's authority, the manager can approve the application directly. Otherwise, the application will be handed over to the Director. The director can handle all requests in the same way. This is a typical responsibility chain model. The request processing forms a chain until there is an object processing request. The UML diagram of this example is given.
The code implementation is relatively simple, as shown below:
// 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) // within the Manager's authority {cout <"manager" <m_name <"approval" <name <"salary increase" <num <"Yuan" <Endl <<Endl ;} else {cout <"manager" <m_name <"cannot be processed, and it is handled by 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 & lt; 1000) // within the authority of the Director, {cout <"" <m_name <"" <name <"" <num <"" <Endl <<Endl ;} else {cout <"" <m_name <"cannot be handled, but it is handled by the General Manager" <Endl; m_manager-> dealwithrequest (name, num );}}}; // general manager class generalmanager: Public Manager {public: generalmanager (Manager * manager, string name): Manager (Manager, name) {} void dealwithrequest (string name, int num) // The general manager can handle all requests {cout <"General Manager" <m_name <"approval" <name <"salary increase" <num <"Yuan" <endl <Endl ;}};
The customer calls the following methods:
// Test case int main () {manager * General = new generalmanager (null, "A"); // set the upper level. The general manager does not have the upper-level manager * majordomo = new majordomo (general, "B"); // set the upper-level manager * Common = new commonmanager (Majordomo, "C"); // set the upper-level common-> dealwithrequest ("D", 300 ); // employee D requires a salary increase common-> dealwithrequest ("e", 600); Common-> dealwithrequest ("F", 1000); Delete common; Delete majordomo; Delete general; return 0 ;}
I enjoy the copyright of blog articles, reprint please indicate the source http://blog.csdn.net/wuzhekai1985