The level is limited. please correct me more!
Decorator example
The example is adapted from page 214 of "designer mode analysis (second edition)", people's post and telecommunications Publishing House
Intent:Dynamically add roles to an object, that is, the "plug-and-play" method is provided.Recompile the existing part.
Problem: The object to be used performs the required basic functions. However, you may needSuch as adding some features, these additional features may occur in the basic functions of the object.Before or after.
Solution:You can extend the functions of an object without creating a subclass.
Advantages:The size and complexity of the class hierarchy are greatly reduced.
Disadvantages:
1) if the modifier is decorated, access the features introduced in the decoration mode.It will be very difficult or even dangerous.
2) the system is sensitive to the use sequence of the decorator.
Note: Obviously, the decorator mode is unavailable if you want to change the basic functions of the decorator..
References:
[1] Analysis of designer patterns (second edition), translated by Xu Yansheng, published by People's post and telecommunications press, and Alan shalloway
[2] guide for beginners in design patterns, translated by Allen Holub, Mechanical Industry Press
Description of the example:
1) the specific component salesticket (that is, the decorator) implements the basic functions (that is, printing the ticket body) and requiresAdd a header and a table tail to the table, so use two specific decorative class headers and footer to complete the appending.Function.
2) The example is adapted from the reference document [1]. You can see it if you do not understand it.
3) use the vc6.0 compiler.
# Include <iostream> </P> <p> using namespace STD; </P> <p> // ================================== ============================< br/> Class component // abstract component, that is, the interface of the object <br/>{< br/> Public: <br/> virtual void prtticket () = 0; <br/> }; <br/> // ======================================== ==================< br/> // specific components, decorator <br/> class salesticket: public component <br/>{< br/> Public: <br/> // salesticket () <br/> // {<br/> // cout <"construct a specific component salest Icket/N "<Endl; <br/> //} </P> <p> void prtticket () <br/>{< br/> cout <"Specific Component salesticket prints the ticket body/N" <Endl; <br/>}< br/> }; <br/> // ======================================== ==================< br/> // modifier (also a subclass of abstract components) <br/> class ticketdecorator: public component <br/>{< br/> PRIVATE: <br/> component * pmytrailer; // abstract component as a member variable </P> <p> Public: <br/> ticketdecorator (component & mycomponent) // in actual use, the input constructor is a specific component (decorator) <br/> {<Br/> // cout <"constructor ticketdecorator/N" <Endl; <br/> pmytrailer = NULL; <br/> pmytrailer = & mycomponent; <br/>}</P> <p> void calltrailer () <br/>{< br/> If (pmytrailer! = NULL) <br/> pmytrailer-> prtticket (); <br/>}</P> <p> // Do You Want To release pmytrailer ?????? <Br/> // if you follow the principle of "who applies and who releases", you do not need to <br/> }; <br/> // ======================================== ==================< br/> // The specific modifier header (a subclass of the modifier) <br/> // function: print the header <br/> Class header: Public ticketdecorator <br/>{< br/> Public: <br/> header (component & mycomponent): ticketdecorator (mycomponent) <br/>{< br/> // cout <"construct the specific modifier Header/N" <Endl; <br/>}</P> <p> void prtticket () <br/>{< br/> // function: add a header in front of the table </P> <p> // note this line <SPAN class = 'Wp _ keywordlink'> Code </span>, in calltrailer () previously <br/> // This is a function added by the decorator <br/> cout <"print the header of a specific decorator/N" <Endl; </P> <p> ticketdecorator: calltrailer (); <br/>}< br/> }; </P> <p> // specifies the modifier footer (a subclass of the modifier) <br/> // function: print the end of the table <br/> class footer: public ticketdecorator <br/>{< br/> Public: <br/> footer (component & mycomponent): ticketdecorator (mycomponent) <br/> {<br/> // cout <"construct a specific modifier footer/N" <Endl; <br/>}</P> <p> void P Rtticket () <br/>{< br/> // function: Add a tail behind the table </P> <p> ticketdecorator: calltrailer (); </P> <p> // pay attention to the location of this line of code, in calltrailer () later <br/> // This is the function added by the decorator <br/> cout <"Specific decorator footer prints the end of the table/N" <Endl; <br/>}< br/> }; <br/> // ======================================== ==================< br/> class factory // factory <br/>{< br/> public: <br/> component * m_pmycomponent; <br/> component * m_pmyfooter; <br/> component * m_pmyheader; </P> <p> Factory () <BR/>{< br/> component * m_pmycomponent = NULL; <br/> component * m_pmyfooter = NULL; <br/> component * m_pmyheader = NULL; <br/>}</P> <p> component * getcomponent () <br/>{< br/> m_pmycomponent = new salesticket (); <br/> m_pmyfooter = new footer (* m_pmycomponent); <br/> m_pmyheader = new header (* m_pmyfooter); </P> <p> return m_pmyheader; // returns the last pointer <br/>}</P> <p> ~ Factory () // do not forget to release space <br/>{</P> <p> Delete m_pmycomponent; <br/> Delete m_pmyfooter; <br/> Delete m_pmyheader; <br/>}< br/> }; <br/> // ======================================== ====================</P> <p> int main (INT argc, char * argv []) <br/>{< br/> // use <br/> factory myfactory; <br/> component * pmycomponent = myfactory. getcomponent (); <br/> pmycomponent-> prtticket (); // print </P> <p> return 0; <br/>}</P> <p>