结构型--装饰者模式

前言 : 装饰一个东西, 我认为就是为它增加一些东西, 它还是它, 只是多了一些装饰 .

装饰者模式

用礼物这个概念可以很直观的描述, 直接上代码 :


礼物

// 礼物接口
public interface Present {
	void printMe();
	void printCost();
	int cost();
}
// 具体礼物
public class Iphone implements Present {
	@Override
	public void printMe() {
		System.out.println("I am present iphone .");
	}
	@Override
	public void printCost() {
		System.out.println("total cost  [ " + this.cost() + " ]");
	}
	@Override
	public int cost() {
		return 6000;
	}
}

装饰者 :

// 需要实现被装饰的接口
public interface Decorator extends Present {
}

具体装饰者 :

// 纸质的盒子
public class PaperBoxDecorator implements Decorator {
	private Present present;
	public PaperBoxDecorator(Present present) {
		this.present = present;
	}
	@Override
	public void printMe() {
		present.printMe();
		System.out.println("Add a paper box for the present .");
	}
	@Override
	public void printCost() {
		System.out.println("total cost  [ " + this.cost() + " ]");
	}
	@Override
	public int cost() {
		return present.cost() + 10;
	}
}
// 铁质的盒子
public class SteelBoxDecorator implements Decorator {
	private Present present;
	public SteelBoxDecorator(Present present) {
		this.present = present;
	}
	@Override
	public void printMe() {
		present.printMe();
		System.out.println("Add a steel box for the present .");
	}
	@Override
	public void printCost() {
		System.out.println("total cost  [ " + this.cost() + " ]");
	}
	@Override
	public int cost() {
		return present.cost() + 50;
	}
}
// 一朵小花
public class FlowerDecorator implements Decorator {
	private Present present;
	public FlowerDecorator(Present present) {
		this.present = present;
	}
	@Override
	public void printMe() {
		present.printMe();
		System.out.println("Add one flower for the present .");
	}
	@Override
	public void printCost() {
		System.out.println("total cost  [ " + this.cost() + " ]");
	}
	@Override
	public int cost() {
		return present.cost() + 5;
	}
}

客户端使用时 :

public class Client {
	public static void main(String[] args) {
		// 只买一个iphone
		Present present = new Iphone();
		present.printMe();
		present.printCost();
		
		// 用铁盒装饰并附上一朵花
		present = new SteelBoxDecorator(present);
		present = new FlowerDecorator(present);
		
		present.printMe();
		present.printCost();
		
		// 用纸盒装饰并附上5朵花
		Present presentB = new Iphone();
		presentB = new PaperBoxDecorator(presentB);
		presentB = new FlowerDecorator(presentB);
		presentB = new FlowerDecorator(presentB);
		presentB = new FlowerDecorator(presentB);
		presentB = new FlowerDecorator(presentB);
		presentB = new FlowerDecorator(presentB);
		
		presentB.printMe();
		presentB.printCost();
	}
}

输出结果为 :

I am present iphone .
total cost  [ 6000 ]
I am present iphone .
Add a steel box for the present .
Add one flower for the present .
total cost  [ 6055 ]
I am present iphone .
Add a paper box for the present .
Add one flower for the present .
Add one flower for the present .
Add one flower for the present .
Add one flower for the present .
Add one flower for the present .
total cost  [ 6035 ]

优点

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

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

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

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

缺点

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

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

装饰者的使用场景

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值