Syong : 装饰者模式

装饰者模式

What
装饰者模式(Decorator Pattern):属于结构型模式。

维基百科:In object-oriented programming, the decorator pattern is a design pattern that allows behavior to be added to an individual object, dynamically, without affecting the behavior of other objects from the same class. The decorator pattern is often useful for adhering to the Single Responsibility Principle, as it allows functionality to be divided between classes with unique areas of concern. The decorator pattern is structurally nearly identical to the chain of responsibility pattern, the difference being that in a chain of responsibility, exactly one of the classes handles the request, while for the decorator, all classes handle the request.

标红那句翻译过来就是:它允许将行为动态地添加到单个对象中,而不会影响同一类中其他对象的行为。

个人理解:向一个现有对象增强/拓展/增加其功能,不用改变现有对象。动态的意思是可以动态地选择装饰者,如果把糖果比喻成现有对象,那么包装纸可以比喻成装饰者,有多款不同包装纸,我们可以动态选择不同款的包装纸。

ps:

  1. 适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式是一种结构型模式;

How
示例:给糖果包装多层包装纸

//测试类
public class Test {
    public static void main(String []args) {
		Sugar sugar = new Lollipop();
		SugarDecorator sugarDecorator = new AWrapperDecorator(sugar);
		sugarDecorator = new BWrapperDecorator(sugarDecorator);
		String outward = sugarDecorator.show();
		System.out.println(outward);
		
		//也可以写成这样
		Sugar sugarAfterWrapper = new BWrapperDecorator(new AWrapperDecorator(new Lollipop()));
		System.out.println(sugarAfterWrapper.show());
    }
}
interface Sugar{
	String show();
}
//棒棒糖
class Lollipop implements Sugar{
	@Override
	public String show() {
		return "Sugar:[Colorful]";
	}
}
//糖果装饰者抽象类
abstract class SugarDecorator implements Sugar{
	private Sugar sugar;
	public SugarDecorator( Sugar sugar){
		this.sugar = sugar;
	}
	@Override
	public String show() {
		return this.sugar.show();
	}
}
//A类包装纸-装饰者具体类
class AWrapperDecorator extends SugarDecorator {
	public AWrapperDecorator ( Sugar sugarDecorator ) {
		super(sugarDecorator);
	}
	@Override
	public String show() {
		return "AWrapper:[Red] " + super.show() + " AWrapper:[Red]";
	}
}
//A类包装纸-装饰者具体类
class BWrapperDecorator extends SugarDecorator {
	public BWrapperDecorator ( Sugar sugarDecorator ) {
		super(sugarDecorator);
	}
	@Override
	public String show() {
		return "BWrapper:[White] " + super.show() + " BWrapper:[White] ";
	}
}

运行结果:
在这里插入图片描述

运行结果如图:
在这里插入图片描述
棒棒糖被红色包装纸包裹,然后被白色包装纸包裹(白色不太明显~就这样),有点递归的意思

这种写法有没有很熟悉Sugar sugarAfterWrapper = new BWrapperDecorator(new AWrapperDecorator(new Lollipop()));
没错,就是java.io里面的InputStream与OutputStream用的也是装饰者类:
outputStream = new BufferedOutputStream(new FileOutputStream(new File("test2.txt")));
上面那句代码怎么去理解呢?
从里到外:1.打开一个文件 2.打开写文件流 3.加入缓冲流让写更快

缺点

装饰者实现类可能会很多,容易出现类膨胀,需要维护的代码就要多一些, 并且它们之间往往长得都很相似,比如java中的InputStream类,如果对这些装饰者类不熟悉的话,可能一时间会陷入不知道该使用哪个装饰者的尴尬境地。

When & Where
上面 java.io 就是一个应用例子;

参考 : 装饰者模式

结束语:优秀是一种习惯。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值