This is Bwar in 2009 to write the design pattern C + + implementation, has been existing in their own computer, has been sharing in the team technology sharing, is now moving to the line.
1. Decoration Mode Description1.1 Purpose
add some extra responsibilities to an object dynamically.
1.2 Applicability
(1) Add responsibilities to a single object in a dynamic, transparent manner without affecting other objects.
(2) to deal with duties that can be canceled.
(3) It is not possible or bad to use the method of generating subclasses to expand responsibilities.
2. Decoration Mode Structure Chart
- Component: Defines an object interface that can dynamically add responsibilities to these objects.
- Concretecomponent: Defines an object that can add some responsibility to the object.
- Decorator: Maintains a pointer to component object only, and defines an interface that is consistent with the component interface.
- Concretedecorator: Add responsibilities to the build.
3. Examples of application scenarios for decorative modes3.1 Program Log class
- File Log Class
- Console Log Class
- Database log Classes
- Network Log Class
The user only calls the Write log interface, where the log is written, how to write is not the user's concern, the location of the log and content changes are not concerned.
3.2 Database proxy classes
- MySQL Data agent
- Oracle Data Agent
- SQL Server Data Agent
- Network Data Relay proxy class
The user only wants to perform basic database query, insert, take result operation, SQL conforms to specification, operation of what database, distribution where is not his concern; even if the database is changed from SQL Server to MySQL, there is no need to change the code if the library table structure is unchanged.
4. Decoration Mode C + + Implementation Example
There are two backpacks: a sports backpack (usually playing basketball, playing badminton back), an outdoor backpack (mountain climbing, hiking back), when going out occasionally on the backpack hanging some ornaments.
Code implementation:
BAG.HPP:
#ifndef Bag_hpp_ #define Bag_hpp_<iostream>usingnamespace std; class cbag{public: cbag () {} virtual ~cbag () {} Virtualvoid0;}; #endif /* BAG_HPP_ */
SPORTBAG.HPP:
#ifndef Sportbag_hpp_#defineSportbag_hpp_#include<iostream>#include"bag.hpp"classCsportbag: Publiccbag{ Public: Csportbag () {}Virtual~csportbag () {}Virtual voidoperation () {cout<<"Sport Bag"; }};#endif/* SPORTBAG_HPP_ */
OUTDOORBAG.HPP:
#ifndef Outdoorbag_hpp_#defineOutdoorbag_hpp_#include<iostream>#include"bag.hpp"classCoutdoorbag: Publiccbag{ Public: Coutdoorbag () {}Virtual~coutdoorbag () {}Virtual voidoperation () {cout<<"Outdoor Bag"; }};#endif/* OUTDOORBAG_HPP_ */
DECORATORBAG.HPP:
#ifndef Decoratorbag_hpp_#defineDecoratorbag_hpp_#include"bag.hpp"classCdecoratorbag: Publiccbag{ Public: Cdecoratorbag (cbag*pbag) {M_pbag=Pbag; } Virtual~cdecoratorbag () {}Virtual voidoperation () {M_pbag-operation (); }Private: Cbag*m_pbag;};#endif/* DECORATORBAG_HPP_ */
QMMDECORATORBAG.HPP:
#ifndef Qmmdecoratorbag_hpp_#defineQmmdecoratorbag_hpp_#include<iostream>#include"decoratorbag.hpp"classCqmmdecoratorbag: Publiccdecoratorbag{ Public: Cqmmdecoratorbag (cbag*pbag): Cdecoratorbag (pbag) {}Virtual~cqmmdecoratorbag () {}Virtual voidoperation () {cdecoratorbag::operation (); Hang (); }protected: voidHang () {cout<<"With accouterment QMM."<<Endl; }};#endif/* QMMDECORATORBAG_HPP_ */
QGGDECORATORBAG.HPP:
#ifndef Qggdecoratorbag_hpp_#defineQggdecoratorbag_hpp_#include<iostream>#include"decoratorbag.hpp"classCqggdecoratorbag: Publiccdecoratorbag{ Public: Cqggdecoratorbag (cbag*pbag): Cdecoratorbag (pbag) {}Virtual~cqggdecoratorbag () {}Virtual voidoperation () {cdecoratorbag::operation (); Hang (); }protected: voidHang () {cout<<"With accouterment Qgg."<<Endl; }};#endif/* QGGDECORATORBAG_HPP_ */
DecoratorMain.cpp:
#include <iostream>#include"bag.hpp"#include"sportbag.hpp"#include"outdoorbag.hpp"#include"decoratorbag.hpp"#include"qggdecoratorbag.hpp"#include"qmmdecoratorbag.hpp"using namespacestd;intMain () {Cbag*bag; Cbag* Sportbag =Newcsportbag (); Cbag* Outdoorbag =Newcoutdoorbag (); Bag=Newcqggdecoratorbag (sportbag); Bag-operation (); Deletebag; DeleteSportbag; DeleteOutdoorbag; return 0;}
C + + implementation of design patterns-Decorative mode