The decorator mode is to implement the decoration entity, and then a number of decorator methods can be used to dynamically decorate the decorator. Avoid using inheritance by using a combination. It can be dynamically expanded and relatively flexible, but the disadvantage is that there are many classes to implement.
650) this. width = 650; "title =" qq 30908011408.jpg "src =" http://www.bkjia.com/uploads/allimg/131228/2021143406-0.jpg "/>
The Componet decorator mode defines a base class of the decorator to implement the actions of the decorator and then implement the specific object of the decorator.
Based on the base class of the Decorator, Decorator implements the base class of the Decorator. The base class of the Decorator inherits the base class of the Decorator, this base class implements the interface to be implemented by the modifier. You can modify the existing interface.
After that, a specific modifier saves the Entity objects of the decorator to be decorated, and then calls the method to decorate the decorator.
The following uses a simple example to implement the decorator mode. First, we define a base class for the decorator.
After that, the entity of each decorated person will be a person or a person from Shandong Province.
Define a modifier base class after, inherit the modifier base class, and modify its interface. Regulate the behavior of the entity object of the modifier.
Finally, define the specific modifier, which saves the object to be decorated, and implements the decoration of the decorator.
The Code is as follows:
Decorator virtual base class
/** Decorator virtual base class **/class CHuman {public: // here, Info is the virtual function of the decorator, however, to modify this method, the decorator can use the decorator's virtual base class to inherit the virtual base class of the decorator and re-implement its original method virtual string Info () {m_sInfo = "No information"; return m_sInfo;} // saves the virtual function. The derived class must implement virtual int Leavl () = 0; protected: string m_sInfo ;};
Implemented by the decorator
/** Specific implementation class of the decorator **/class CLiaoningHuman: public CHuman {public: CLiaoningHuman () {m_sInfo = "";} virtual int Leavl () {return 0 ;}};
Implemented by the decorator
/** Specific implementation class of the decorator **/class CShanDongHuman: public CHuman {public: CShanDongHuman () {m_sInfo = "Shandong";} virtual int Leavl () {return 0 ;}};
Decorator virtual base class
/** The Decorator virtual base class defines the interface that must be implemented by the Decorator object and modifies the base class interface of the Decorator. **/class Decorator: public CHuman {public:/** inherits the main interface of the decorator and modifies the interface to be modified. it overwrites the Info interface of the decorator, and implement **/virtual string Info () = 0 ;}on a specific modifier object ;};
Decoration object Wudang martial arts
/** Implementation class of the modifier **/class CDecratorWuDang: public Decorator {public: CDecratorWuDang (CHuman * pCHuman) {m_pCHuman = pCHuman ;} // implement the Save virtual function of the decorator base class (this interface modifies the virtual base class interface of the decorator) virtual string Info () {return m_pCHuman-> Info () + "Wu Dang Kung Fu";} virtual int Leavl () {return m_pCHuman-> Leavl () + 1;} private: // Save the specific CHuman * m_pCHuman ;};
Decoration entity object Shaolin martial arts
/** Implementation class of the modifier **/class CDecratorShaoLin: public Decorator {public: CDecratorShaoLin (CHuman * pCHuman) {m_pCHuman = pCHuman ;} // implement the Save virtual function of the decorator base class (this interface modifies the virtual base class interface of the decorator) virtual string Info () {return m_pCHuman-> Info () + "Shaolin kung fu learning";} virtual int Leavl () {return m_pCHuman-> Leavl () + 2;} private: // Save the specific CHuman * m_pCHuman ;};
Decorated object: Huashan wugong
/** Implementation class of the modifier **/class CDecratorShaoLin: public Decorator {public: CDecratorShaoLin (CHuman * pCHuman) {m_pCHuman = pCHuman ;} // implement the Save virtual function of the decorator base class (this interface modifies the virtual base class interface of the decorator) virtual string Info () {return m_pCHuman-> Info () + "Shaolin kung fu learning";} virtual int Leavl () {return m_pCHuman-> Leavl () + 2;} private: // Save the specific CHuman * m_pCHuman ;};
Test Functions
Int main (int argc, char * argv []) {// defines the CHuman * pCHuman = new CLiaoningHuman (); // defines the modifier, and pass in the modifier to decorate it with pCHuman = new trim (pCHuman); pCHuman = new CDecratorShaoLin (pCHuman); pCHuman = new CDecratorHuaShan (pCHuman ); cout <pCHuman-> Info () <endl; cout <pCHuman-> Leavl () <endl; pCHuman = new CShanDongHuman (); pCHuman = new round (pCHuman); pCHuman = new CDecratorShaoLin (pCHuman); cout <pCHuman-> Info () <endl; cout <pCHuman-> Leavl () <endl; cout <endl <"Press any key exit ..... "<endl; char c = getchar (); return 0 ;}
Test Results
650) this. width = 650; "title =" qq 30908013116.jpg "src =" http://www.bkjia.com/uploads/allimg/131228/20211442c-1.jpg "/>
This article is from the "Feng qing yang song" blog, please be sure to keep this source http://2309998.blog.51cto.com/2299998/1291015