设计模式-装饰模式

装饰模式

1)装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案
2)装饰模式可以在不创造更多子类的情况下,将对象的功能加以扩展
3)装饰模式把客户端的调用委派到被装饰类。装饰模式的关键在于这种扩展完全是透明的。
4)装饰模式是在不必改变原类文件和使用继承的情况下,动态的扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。

装饰模式的角色

以java的IO流来说明:
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("data.txt")));
1)抽象构件角色
   给出一个抽象接口,以规范准备接收附加责任的对象。如:OutputStream接口
2)具体构件角色
   定义一个将要接收附加责任的类 如:FileOutputStream节点类
3)装饰角色
  持有一个构件(Component)对象的引用,并定义一个与抽象构件接口一致的接口
  如:FilterOutputStream
4)具体装饰角色
  负责给构件对象“贴上”附加的责任。
  如:BufferedOutputStream等过滤类

装饰模式的特点

1)装饰对象和真实对象有相同的接口。这样客户端对象就可以以和真实对象相同的方式和装饰对象交互。
2)装饰对象包含一个真实对象的引用(reference)
3)装饰对象接收所有来自客户端的请求。它把这些请求转发给真实的对象。
4)装饰对象可以在转发这些请求以前或以后增加一些附加功能。这样就确保了在运行时,不用修改给定对象的结构就可以在外部增加附加的功能。在面向对象的设计中,通常是通过继承来实现对给定类的功能扩展。

实例

简单装饰:
抽象构建角色:
public interface IWorker {
	public void doSomething();
}

装饰角色:
public class ColourWorker implements IWorker {
	private IWorker worker;//必须持有抽象构建角色
	public ColourWorker(IWorker worker){
		this.worker = worker;
	}
	@Override
	public void doSomething() {
		System.out.println("上色前");
		this.worker.doSomething();
		System.out.println("上色后");
	}
}

public class WriteWorker  implements IWorker {
	private IWorker worker;//必须持有抽象构建角色
	public WriteWorker(IWorker worker){
		this.worker = worker;
	}
	@Override
	public void doSomething() {
		System.out.println("题字前");
		this.worker.doSomething();
		System.out.println("题字后");
	}

}

客户端
public class Test {

	public static void main(String[] args) {
		IWorker iWorker = new WriteWorker(new ColourWorker(new BaseWoker()));
		iWorker.doSomething();
//		print
//		题字前
//		上色前
//		打卡
//		上色后
//		题字后
	}
}

java的IO模拟实例:
抽象构建角色
public interface Component {
	public void doSomething();
}

具体构建功能(个人理解其实就是一个最底层的修饰)
public class ConcreteComponent implements Component {

	@Override
	public void doSomething() {
		System.out.println("基本功能");
	}

}

装饰角色
public class Decorator implements Component {
	private Component component;

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

	@Override
	public void doSomething() {
		component.doSomething();
	}

}

具体装饰角色A
public class ConcreteDecoratorA extends Decorator {
	
	public ConcreteDecoratorA(Component component) {
		super(component);
	}

	@Override
	public void doSomething() {
		super.doSomething();
		this.doAnotherThing();
	}

	private void doAnotherThing() {
		System.out.println("功能A");
	}

}

具体装饰角色B
public class ConcreteDecoratorB extends Decorator {
	public ConcreteDecoratorB(Component component) {
		super(component);
	}

	@Override
	public void doSomething() {
		super.doSomething();
		this.doAnotherThing();
	}

	private void doAnotherThing() {
		System.out.println("功能B");
	}

}

客户端
public class Test {
	public static void main(String[] args) {
		// 节点流
		Component component = new ConcreteComponent();
		// 过滤流 
		Component componentA = new ConcreteDecoratorA(component);
		componentA.doSomething();
		System.out.println("------------");
		// 过滤流
		Component componentB = new ConcreteDecoratorB(componentA);
		componentB.doSomething();
		Component test = new ConcreteDecoratorA(new ConcreteDecoratorB(
				new ConcreteComponent()));
		test.doSomething();
//		基本功能
//		功能A
//		------------
//		基本功能
//		功能A
//		功能B
//		基本功能
//		功能B
//		功能A
	}

}

装饰VS继承

装饰模式
–用来扩展特定对象的功能
–不需要子类
–动态
–运行时分配职责
–防止由于子类而导致的复杂和混乱
–更多的灵活性
–对于一个给定的对象,同时可能有不同的装饰
继承
–用来扩展一类对象的功能
–需要子类
–静态
–编译时分派职责
–导致很多子类产生
–缺乏灵活性




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值