设计模式 - Decorator模式(装饰模式)

 

Decorator模式的作用:

态地给一个对象添加一些额外的职责。就像在墙上刷油漆。就增加功能来说,Decorator 模式相比生成子类更为灵活。Decorator常被翻译成"装饰",我觉得翻译成"油漆工"更形象点,油漆工(decorator)是用来刷油漆的,那么被刷油漆的对象我们称decoratee.这两种实体在Decorator模式中是必须的.

 

为什么使用Decorator?

刷油漆的对象我们称decoratee.这两种实体在Decorator模式中是必须的  我们通常可以使用继承来实现功能的拓展,如果这些需要拓展的功能的种类很繁多,那么势必生成很多子类,增加系统的复杂性,同时,使用继承实现功能拓展,我们必须可预见这些拓展功能,这些功能是编译时就确定了,是静态的。

 

 

使用Decorator的理由是:

这些功能需要由用户动态决定加入的方式和时机.Decorator提供了"即插即用"的方法,在运行期间决定何时增加何种功能. 

 

UML结构图:

 

抽象基类:

1)Component:定义一个对象接口,可以为这个接口动态的添加职责。

2)Decorator:维持一个指向Component的指针,并且有一个和Component一致的接口函数。

接口函数:

1)Component::Operation:这个接口函数由Component声明,因此Component的派生类都需要实现,可以在这个接口函数的基础上给它动态添加职责。

解析:

Decorator的派生类可以为ConcreateComponent类的对象动态的添加职责,或者可以这么说:Decorator的派生类装饰ConcreateComponent类的对象.具体是这么实现的,首先初始化一个ConcreateComponent类的对象(被装饰者),采用这个对象去生成一个Decorator对象(装饰者),之后对Operation函数的调用则是对这Decorator对象成员函数的多态调用.这里的实现要点是Decorator类和ConcreateComponent类都继承自Component,从而两者的接口函数是一致的;其次,Decorator维护了一个指向Component的指针,从而可以实现对Component::Operation函数的动态调用。

 

实现代码:


Decorator.h
 1 #ifndef DECORATOR_H
 2 #define DECORATOR_H
 3 
 4 // 抽象基类,定义一个对象接口,可以为这个接口动态的添加职责.
 5 class Component
 6 {
 7 public:
 8     Component(){}
 9     virtual ~Component(){}
10 
11     // 纯虚函数,由派生类实现
12     virtual void Operation() = 0;
13 };
14 
15 // 抽象基类,维护一个指向Component对象的指针
16 class Decorator
17     : public Component
18 {
19 public:
20     Decorator(Component* pComponent) : m_pComponent(pComponent){}
21     virtual ~Decorator();
22 
23 protected:
24     Component* m_pComponent;
25 };
26 
27 // 派生自Component,在这里表示需要给它动态添加职责的类
28 class ConcreateComponent
29     : public Component
30 {
31 public:
32     ConcreateComponent(){}
33     virtual ~ConcreateComponent(){}
34 
35     virtual void Operation();
36 };
37 
38 // 派生自Decorator,这里代表为ConcreateComponent动态添加职责的类
39 class ConcreateDecorator
40     : public Decorator
41 {
42 public:
43     ConcreateDecorator(Component* pComponent) : Decorator(pComponent){}
44     virtual ~ConcreateDecorator(){}
45 
46     virtual void Operation();
47 
48 private:
49     void AddedBehavior();
50 };
51 
52 #endif
53 

 


Decorator.cpp
 1 
 2 #include "Decorator.h"
 3 #include <iostream>
 4 
 5 Decorator::~Decorator()
 6 {
 7     delete m_pComponent;
 8     m_pComponent = NULL;
 9 }
10 
11 void ConcreateComponent::Operation()
12 {
13     std::cout << "Operation of ConcreateComponent\n";
14 }
15 
16 void ConcreateDecorator::Operation()
17 {
18     m_pComponent->Operation();
19     AddedBehavior();
20 }
21 
22 void ConcreateDecorator::AddedBehavior()
23 {
24     std::cout << "AddedBehavior of ConcreateDecorator\n";
25 }
26 

 


 

Main.cpp
 1 
 2 #include "Decorator.h"
 3 #include <stdlib.h>
 4 
 5 int main()
 6 {
 7     // 初始化一个Component对象
 8     Component* pComponent = new ConcreateComponent();
 9     // 采用这个Component对象去初始化一个Decorator对象,
10     // 这样就可以为这个Component对象动态添加职责
11     Decorator* pDecorator = new ConcreateDecorator(pComponent);
12 
13     pDecorator->Operation();
14 
15     delete pDecorator;
16 
17     system("pause");
18 
19     return 0;
20 }
21 

 

 

posted @ 2010-12-02 11:51  忧郁的加菲猫  阅读(409)  评论(0编辑  收藏  举报