装饰器模式(Decorator)

一句话:动态扩展对象的功能。

定义:

装饰者模式又名包装(Wrapper)模式。装饰者模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案。

装饰者模式动态地将责任附加到对象身上。若要扩展功能,装饰者提供了比继承更有弹性的替代方案。

需要解决的问题:

一般的,我们为了扩展一个类经常使用继承方式实现,由于继承为类引入静态特征,并且随着扩展功能的增多,子类会很膨胀。

如何解决:

将具体功能职责划分,同时继承装饰者模式。

优点: 

1、装饰者模式可以提供比继承更多的灵活性

2、可以通过一种动态的方式来扩展一个对象的功能,在运行时选择不同的装饰器,从而实现不同的行为。

3、通过使用不同的具体装饰类以及这些装饰类的排列组合,可以创造出很多不同行为的组合。可以使用多个具体装饰类来装饰同一对象,得到功能更为强大的对象。

4、具体构件类与具体装饰类可以独立变化,用户可以根据需要增加新的具体构件类和具体装饰类,在使用时再对其进行组合,原有代码无须改变,符合“开闭原则”。

缺点:

1、会产生很多的小对象,增加了系统的复杂性

2、这种比继承更加灵活机动的特性,也同时意味着装饰模式比继承更加易于出错,排错也很困难,对于多次装饰的对象,调试时寻找错误可能需要逐级排查,较为烦琐。

扫描二维码关注公众号,回复: 3302925 查看本文章

使用场景:

1、在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。

2、需要动态地给一个对象增加功能,这些功能也可以动态地被撤销。  当不能采用继承的方式对系统进行扩充或者采用继承不利于系统扩展和维护时。

应用实例:

装饰模式在Java语言中的最著名的应用莫过于Java I/O标准库的设计了。

由于Java I/O库需要很多性能的各种组合,如果这些性能都是用继承的方法实现的,那么每一种组合都需要一个类,这样就会造成大量性能重复的类出现。而如果采用装饰模式,那么类的数目就会大大减少,性能的重复也可以减至最少。因此装饰模式是Java I/O库的基本模式。

Java I/O库的对象结构图如下,由于Java I/O的对象众多,因此只画出InputStream的部分。

根据上图可以看出:

  • 抽象构件(Component)角色:由InputStream扮演。这是一个抽象类,为各种子类型提供统一的接口。
  • 具体体构件(ConcreteComponent)角色:由ByteArrayInputStream、FileInputStream、PipedInputStream、StringBufferInputStream等类扮演。它们实现了抽象构件角色所规定的接口。
  • 抽象装饰(Decorator)角色:由FilterInputStream扮演。它实现了InputStream所规定的接口。
  • 具体装饰(ConcreteDecorator)角色:由几个类扮演,分别是BufferedInputStream、DataInputStream以及两个不常用到的类LineNumberInputStream、PushbackInputStream。

UML类图:

在装饰模式中的角色有:

  •   抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加责任的对象。
  •   具体构件(ConcreteComponent)角色:定义一个将要接收附加责任的类。
  •   装饰(Decorator)角色:持有一个构件(Component)对象的实例,并定义一个与抽象构件接口一致的接口。
  •   具体装饰(ConcreteDecorator)角色:负责给构件对象“贴上”附加的责任。

 demo:

1.抽象构件角色

package com.pattern.decorator;

public interface Component {
	public void sampleOperation();
}

2.具体构件角色

package com.pattern.decorator;

public class ConcreteComponent implements Component {
    @Override
    public void sampleOperation() {
    	System.out.println("the original method!");
    }
}

3.装饰角色

package com.pattern.decorator;

public class Decorator implements Component {
	private Component component;

	public Decorator(Component component) {
		this.component = component;
	}

	@Override
	public void sampleOperation() {
		// 委派给构件
		component.sampleOperation();
	}
}

4.具体装饰角色1

package com.pattern.decorator;

public class ConcreteDecorator1 extends Decorator {
	public ConcreteDecorator1(Component component) {
		super(component);
	}

	@Override
    public void sampleOperation() {
        System.out.println("before decorator1!");  
		super.sampleOperation();
        System.out.println("after decorator1!"); 
    }
}

5.具体装饰角色2

package com.pattern.decorator;

public class ConcreteDecorator2 extends Decorator {
	public ConcreteDecorator2(Component component) {
		super(component);
	}

	@Override
    public void sampleOperation() {
        System.out.println("before decorator2!");  
		super.sampleOperation();
        System.out.println("after decorator2!"); 
    }
}

6.测试类

package com.pattern.decorator;

public class Client {

	public static void main(String[] args) {
		Component source = new ConcreteComponent();  
		Component obj1 = new ConcreteDecorator2(new ConcreteDecorator1(source));  
        obj1.sampleOperation(); 
	}

}

猜你喜欢

转载自blog.csdn.net/haoxin963/article/details/82355631