装饰者模式
Decorator模式(别名Wrapper)动态将职责附加到对象上,若要扩展功能,装饰者提供了比继承更具弹性的代替方案。
动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活。
设计原则
- 多用组合,少用继承。
利用继承设计子类的行为,是在编译时静态决定的,而且所有的子类都会继承到相同的行为。然而,如果能够利用组合的做法扩展对象的行为,就可以在运行时动态地进行扩展。
- 类应设计的对扩展开放,对修改关闭。
继承的缺点:
- 对于可组合的类会产生类爆炸效果
比如程序猿类,有Android,Ios,Web等多种技能,定义一个程序猿,可能会Android程序猿,Ios程序猿,Web程序猿,会IOS和Android的程序猿,会IOS和Web的程序猿…,采用继承的方式,会导致子类无穷无尽,尤其是技能的类型变多之后。
子类不想要父类的行为但又不得不继承
比如鸭子类,父类包含一个guagua()的行为,但是有个橡皮鸭的子类不会鸣叫,并不需要有这个guagua()的行为,但又不得不继承
子类和父类高度耦合,不便于维护
如果父类增删改方法,就要所有的子类都增删改方法,想想就可怕
适用性
以下情况使用Decorator模式
需要扩展一个类的功能,或给一个类添加附加职责。
需要动态的给一个对象添加功能,这些功能可以再动态的撤销。
需要增加由一些基本功能的排列组合而产生的非常大量的功能,从而使继承关系变的不现实。
当不能采用生成子类的方法进行扩充时。一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。另一种情况可能是因为类定义被隐藏,或类定义不能用于生成子类。
优缺点
优点
Decorator模式与继承关系的目的都是要扩展对象的功能,但是Decorator可以提供比继承更多的灵活性。
通过使用不同的具体装饰类以及这些装饰类的排列组合,设计师可以创造出很多不同行为的组合。
缺点
这种比继承更加灵活机动的特性,也同时意味着更加多的复杂性。
装饰模式会导致设计中出现许多小类,如果过度使用,会使程序变得很复杂。
装饰模式是针对抽象组件(Component)类型编程。但是,如果你要针对具体组件编程时,就应该重新思考你的应用架构,以及装饰者是否合适。当然也可以改变Component接口,增加新的公开的行为,实现“半透明”的装饰者模式。在实际项目中要做出最佳选择。
要点
1. 装饰者和被装饰对象有相同的超类型
2. 可以用一个或多个装饰者包装一个对象。
3. 装饰者可以在所委托被装饰者的行为之前或之后,加上自己的行为,以达到特定的目的。
4. 对象可以在任何时候被装饰,所以可以在运行时动态的,不限量的用你喜欢的装饰者来装饰对象。
5. 装饰模式中使用继承的关键是想达到装饰者和被装饰对象的类型匹配,而不是获得其行为。
6. 装饰者一般对组件的客户是透明的,除非客户程序依赖于组件的具体类型。在实际项目中可以根据需要为装饰者添加新的行为,做到“半透明”装饰者。
7. 适配器模式的用意是改变对象的接口而不一定改变对象的性能,而装饰模式的用意是保持接口并增加对象的职责。
Component:
定义一个对象接口,可以给这些对象动态地添加职责。
public interface Component
{
void operation();
}
Concrete Component:
定义一个对象,可以给这个对象添加一些职责。
public class ConcreteComponent implements Component
{
public void operation()
{
// Write your code here
}
}
Decorator:
维持一个指向Component对象的引用,并定义一个与 Component接口一致的接口。
public class Decorator implements Component
{
public Decorator(Component component)
{
this.component = component;
}
public void operation()
{
component.operation();
}
private Component component;
}
Concrete Decorator:
在Concrete Component的行为之前或之后,加上自己的行为,以“贴上”附加的职责。
public class ConcreteDecorator extends Decorator
{
public void operation()
{
//addBehavior也可以在前面
super.operation();
addBehavior();
}
private void addBehavior()
{
//your code
}
}
举例
一个水果背包,里面可以装各种各样的水果。先定义一个水果背包,然后定义各种水果装饰,在水果背包中想装什么水果就装什么。
背包接口
public interface IBag {
int getWeight();
String getContent();
}
定一个具体的水果背包
public class FruitBag implements IBag{
@Override
public int getWeight() {
return 1;
}
@Override
public String getContent() {
return "FruitBag(" + getWeight() + "kg)";
}
}
水果背包的装饰者
public class FruitDecorator implements IBag {
protected IBag mBag;
protected int mSelfWeight;
public FruitDecorator(IBag bag) {
mBag = bag;
}
@Override
public int getWeight() {
return mBag.getWeight();
}
@Override
public String getContent() {
return mBag.getContent();
}
}
苹果装饰
public class AppleDecorator extends FruitDecorator {
public AppleDecorator(IBag bag) {
super(bag);
mSelfWeight = 2;
}
@Override
public int getWeight() {
return super.getWeight() + mSelfWeight;
}
@Override
public String getContent() {
return super.getContent() + " + Apple(" + mSelfWeight + "kg)";
}
}
梨装饰
public class PearDecorator extends FruitDecorator {
public PearDecorator(IBag bag) {
super(bag);
mSelfWeight = 4;
}
@Override
public String getContent() {
return super.getContent() + " + Pear(" + mSelfWeight + "kg)";
}
@Override
public int getWeight() {
return super.getWeight() + mSelfWeight;
}
}
橘子装饰
public class OrangeDecorator extends FruitDecorator {
public OrangeDecorator(IBag bag) {
super(bag);
mSelfWeight = 3;
}
@Override
public int getWeight() {
return super.getWeight() + mSelfWeight;
}
@Override
public String getContent() {
return super.getContent() + " + Orange(" + mSelfWeight + "kg)";
}
}
测试装饰模式
public class TestDecorator {
public static void main(String args[]){
IBag bag = new FruitBag();
System.out.println("bag Content=" + bag.getContent());
System.out.println("bag weight=" + bag.getWeight() + "kg\n");
bag = new AppleDecorator(bag);
System.out.println("bag Content=" + bag.getContent());
System.out.println("bag weight=" + bag.getWeight() + "kg\n");
bag = new OrangeDecorator(bag);
System.out.println("bag Content=" + bag.getContent());
System.out.println("bag weight=" + bag.getWeight() + "kg\n");
bag = new PearDecorator(bag);
System.out.println("bag Content=" + bag.getContent());
System.out.println("bag weight=" + bag.getWeight() + "kg");
}
}
输出
bag Content=FruitBag(1kg)
bag weight=1kg
bag Content=FruitBag(1kg) + Apple(2kg)
bag weight=3kg
bag Content=FruitBag(1kg) + Apple(2kg) + Orange(3kg)
bag weight=6kg
bag Content=FruitBag(1kg) + Apple(2kg) + Orange(3kg) + Pear(4kg)
bag weight=10kg