概述
装饰模式以对客户端透明的方式为一个对象添加更多的责任,在不需要创建子类的情况下,将对象的功能加以扩展,他的定义如下:
动态地给一个对象增加一些额外的职责,就增加对象功能来说,装饰模式比生成子类实现更为灵活。装饰模式是一种对象结构型模式。UML图:
介绍类图中的角色:
Component:抽象构件,具体构件和抽象装饰的共同父类,声明了具体构件中的业务方法,它的引入可以使客户端以一致的方式处理未被装饰的对象以及装饰之后的对象,实现客户端的透明操作。
ConcreteComponent:具体构件,实现抽象构件中的方法,装饰器可以给他增加额外的方法。
Decorator:抽象装饰类,维护了一个构件对象,可以调用装饰之前构件对象的方法,并通过其子类扩展该方法,以达到装饰的目的。
ConcreteDecorator:具体装饰类,抽象装饰类的子类,负责给构件对象添加新的行为,不同的装饰类定义了不同的行为,它可以调用在抽象装饰类中定义的方法,并可以增加新的方法用以扩充对象的行为。
代码体现
#include<string>
#include<iostream>
using namespace std;
//假设现在客户端需要对一段文字进行加密,根据信息重要程度的不同进行不同程度的加密,一级加密使用base64加密算法,二级再对base64加密结果使用aes加密,三级在二级结果上使用rsa加密
//抽象构件类
class Component
{
public:
virtual std::string encrypt(std::string input) = 0;
};
//具体构件类
class ConcreteComponent : public Component
{
public:
std::string encrypt(std::string input)
{
std::string output;
cout << "此处对input进行了base64加密" << endl;
return output;
}
};
//抽象装饰类
class Decorator : public Component
{
public:
Decorator(Component* comp)
{
component = comp;
}
std::string encrypt(std::string input)
{
return component->encrypt(input);
}
private:
Component * component;
};
//具体装饰类A
class ConcreteDecoratorA : public Decorator
{
public:
ConcreteDecoratorA(Component* Comp)
:Decorator(Comp)
{}
std::string encrypt(std::string input)
{
std::string tmp = Decorator::encrypt(input);
return aesEncrypt(tmp);
}
std::string aesEncrypt(std::string input)
{
std::string output;
cout << "进行了aes加密" << endl;
return output;
}
};
//具体装饰类B
class ConcreteDecoratorB : public Decorator
{
public:
ConcreteDecoratorB(Component* Comp)
:Decorator(Comp)
{}
std::string encrypt(std::string input)
{
std::string tmp = Decorator::encrypt(input);
return rsaEncrypt(tmp);
}
std::string rsaEncrypt(std::string input)
{
std::string output;
cout << "进行了rsa加密" << endl;
return output;
}
};
//使用
int main()
{
//定义具体构件
Component* comp = new ConcreteComponent();
//定义装饰后的构件
Component* decomp = new ConcreteDecoratorA(comp);
decomp->encrypt("123"); //输出:此处对input进行了base64加密 进行了aes加密
//将装饰后的对象继续传入新的装饰类中进行二次装饰
Component* ddecomp = new ConcreteDecoratorB(decomp);
ddecomp->encrypt("123"); //输出:此处对input进行了base64加密 进行了aes加密 进行了rsa加密
system("pause");
return 0;
}
输出:
半透明装饰模式
上面的代码实现的是一个透明的装饰模式,可以对已经装饰过的对象进行再次装饰,但是无法单独调用新增的方法,但实际中会有这种需求,所以半透明装饰模式应运而生。使用:
//具体装饰类B
class ConcreteDecoratorB : public Decorator
{
public:
ConcreteDecoratorB(Component* Comp)
:Decorator(Comp)
{}
std::string rsaEncrypt(std::string input)
{
std::string output;
cout << "进行了rsa加密" << endl;
return output;
}
};
//使用
int main()
{
//定义具体构件
Component* comp = new ConcreteComponent();
ConcreteDecoratorB * rsadec = new ConcreteDecoratorB(comp);
rsadec->encrypt("111");
rsadec->rsaEncrypt("111");
system("pause");
return 0;
}
在半透明装饰类中,具体装饰类不重写父类的方法decrypt,只增加了自己的方法,使用时,对于客户端而言,具体构件类型无须关心,是透明的;但是具体装饰类型必须指定,这是不透明的,如上面的rsadec指针类型制定了是ConcreteDecoratorB类型的指针,这样,rsadec可以单独调用这两种方法。
优缺点和适用场景分析
优点:
1.扩充对象的功能很方便,可以动态增加对象功能,通过配置文件可以在运行时选择不同的具体装饰类,从而实现不同的行为,不会增加系统中类的个数。
2. 可以对一个对象进行多次装饰,通过使用不同的具体装饰类以及这些装饰类的排列组合,可以创造出很多不同行为的组合,得到功能更为强大的对象。
3.具体构件类与具体装饰类可以独立变化,用户可以根据需要增加新的具体构件类和具体装饰类,原有类库代码无须改变,符合“开闭原则”。
缺点:
1.使用装饰模式进行系统设计时将产生很多小对象,这些对象的区别在于它们之间相互连接的方式有所不同,而不是它们的类或者属性值有所不同,大量小对象的产生势必会占用更多的系统资源,在一定程序上影响程序的性能。
2.比继承更加灵活机动,也同时意味着装饰模式比继承更加易于出错,排错也很困难,对于多次装饰的对象,调试时寻找错误可能需要逐级排查,较为烦琐。
适用场景:
1.在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。
2.当不能采用继承的方式对系统进行扩展或者采用继承不利于系统扩展和维护时可以使用装饰模式。不能采用继承的情况主要有两类:第一类是系统中存在大量独立的扩展,为支持每一种扩展或者扩展之间的组合将产生大量的子类,使得子类数目呈爆炸性增长;第二类是因为类已定义为不能被继承(如Java语言中的final类)。