介绍
装饰模式是为已有功能动态地添加更多功能的一种方式,有效地把类的核心职责和装饰功能区分开了,可以去除相关类中重复的装饰逻辑。
装饰模式属于结构型模式,它是作为现有类的一个包装。这种模式创建一个装饰类,用来包装原有的类,并在保持类方法签名完整性的前提下,提供了额外的功能。
使用场景
- 需要扩展一个类的功能,附加功能
- 需要动态给一个对象增加功能,且可以再动态撤销
- 需要为一批兄弟类进行改装或者加装功能
使用注意点
装饰模式的装饰顺序很重要。比如加密数据和过滤词汇都可以是数据持久化前的装饰功能,但是如果先加密再使用过滤功能就会出问题,最理想的情况是保证装饰类之间彼此独立,这样它们就可以以任意的顺序进行组合了。
优缺点
优点:
- 装饰类和被装饰类可以独立发展,不会有耦合关系。装饰类是从外部扩展功能,而不知道具体的构建
- 装饰模式是继承关系的一个代替方案。实现的是is-a关系
- 可以动态的扩展一个类的功能
缺点:
- 多层装饰会有无限剥离的问题,影响效率、性能,工作量大
UML类图
代码实现
tips:例一和例二均参考《大话设计模式》第六章
例一:UML类图的完整实现
#include <iostream>
#include <string>
using namespace std;
class Component
{
public:
virtual void Operation() = 0;
};
class ConcreteComponent : public Component
{
public:
void Operation()
{
cout << "具体对象的操作" << endl;
}
};
class Decorator : public Component
{
protected:
Component *component;
public:
Decorator(){};
Decorator(Component *component)
{
this->component = component;
}
void Operation()
{
if ( this->component != nullptr )
{
component->Operation();
}
}
};
class ConcreteDecoratorA : public Decorator
{
private:
string addState;
public:
ConcreteDecoratorA(Component *c) : Decorator(c) {}
void Operation()
{
Decorator::Operation();
this->addState = "执行本类的功能";
cout << "具体装饰类A对象的操作" << endl;
}
};
class ConcreteDecoratorB : public Decorator
{
private:
//本类独有的方法
void addedBehavior()
{
}
public:
ConcreteDecoratorB(Component *c) : Decorator(c) {}
void Operation()
{
Decorator::Operation();
this->addedBehavior();
cout << "具体装饰类B对象的操作" << endl;
}
};
int main(void)
{
ConcreteComponent *c = new ConcreteComponent();
ConcreteDecoratorA *a = new ConcreteDecoratorA(c);
ConcreteDecoratorB *b = new ConcreteDecoratorB(a);
b->Operation();
return 0;
}
例二:如果只有一个ConcreteComponent类没有抽象的Component类,那么Decorator类可以是ConcreteComponent的一个子类。同样道理,如果只有一个ConcreteDecorator类,那么就没必要建立一个单独的Decorator类,而可以把Decorator和ConcreteDecorator的责任合并成一个类。
#include <iostream>
#include <string>
using namespace std;
class person {
public:
person()
{
}
person(string name)
{
this->name = name;
}
virtual void Show()
{
cout << "装扮的:" << this->name << endl;
}
private:
string name;
};
class Finery : public person
{
protected:
person *component;
public:
Finery() {};
void Decorate(person *component)
{
this->component = component;
}
void Show()
{
component->Show();
}
};
class TShirts : public Finery
{
public:
void Show()
{
cout << "大T恤 ";
Finery::Show();
}
};
class BigTrouser : public Finery
{
public:
void Show()
{
cout << "垮裤 ";
Finery::Show();
}
};
class Sneakers: public Finery
{
public:
void Show()
{
cout << "破球鞋 ";
Finery::Show();
}
};
int main(void)
{
person *xc = new person("小菜");
Sneakers *pqx = new Sneakers();
BigTrouser *kk = new BigTrouser();
TShirts *dtx = new TShirts();
pqx->Decorate(xc);
kk->Decorate(pqx);
dtx->Decorate(kk);
dtx->Show();
}