什么?没看懂?不要紧,最后再来看这个概念,想让让咱们来看一个咖啡屋项目(就是点各式java
各样的咖啡)。测试
本来的设计以下:this
看似很好的设计,可是别忘了,买咖啡时候咱们会让他们给咱们加一系列的调料,例如蒸奶、spa
摩卡......。因此,在这时咖啡店的设计就变成了,以下:设计
不错,你看到了类爆炸!3d
好啦,如今有对这个系统作出了改进,以下:code
好啦,如今这个设计相比以前的的确好了许多,可是若是咱们要修改配料呢?那么就必须修改对象
超类,这时候就违反了一条设计原则:类应该对扩展开放,对修改关闭!blog
因此这个时候,装饰者模式单诞生了!!!!!继承
来看个例子:
若是顾客如今要一杯摩卡和奶泡深培咖啡。那么,要作的是:
①拿一个深培咖啡(DarkRoast)对象
②以摩卡(Mocha)对象装饰它
③以奶泡(Whip)装饰它
④调用cost()方法,并依赖委托(delegate)将调料的几千加上去
如何工做?
①以DarkRoast对象开始
②顾客想要摩卡(Mocha),因此创建一个Mocha对象,并用它将DarkRoast对象包起来
③顾客也想要奶泡(Whip),因此创建一个Whip装饰者,并用它将Mocha对象包起来。别
忘了,DarkRoast继承自Beverage,且有一个cost()方法,用来计算饮料价钱
④如今,该是为顾客算钱的时候了。经过调用最外圈的装饰者的cost就能够办到,以下图:
好了,请记住装饰者和被装饰对象有相同的超类型!如今让咱们写一些代码,了解他的工做吧:
首先看一下咱们如今的设计图:
先从Beverage类下手,这不须要改变原始设计。以下图:
1 public abstract class Beverage { 2 String description = "Unknown Beverage"; 3 4 public String getDescription(){ 5 return description; 6 } 7 8 public abstract double cost(); 9 }
Beverage很简单。让咱们来实现Condiment(调料)抽象类,也就是装饰这类吧:
1 public abstract class CondimentDecorator extends Beverage{ 2 Beverage beverage; 3 4 public abstract String getDescription(); 5 }
如今,该是实现一些饮料的时候了!先从浓缩咖啡(Espresso)开始:
Espresso.java:
1 public class Espresso extends Beverage { 2 public Espresso() { 3 description = "Espresso"; 4 } 5 6 public double cost() { 7 return 1.99; 8 } 9 }
DarkRoast.java:
1 public class DarkRoast extends Beverage { 2 public DarkRoast() { 3 description = "Dark Roast Coffee"; 4 } 5 6 public double cost() { 7 return .99; 8 } 9 }
HouseBlend.java:
1 public class HouseBlend extends Beverage { 2 public HouseBlend() { 3 description = "House Blend Coffee"; 4 } 5 6 public double cost() { 7 return .89; 8 } 9 }
开始协调料代码:
Mocha.java:
1 public class Mocha extends CondimentDecorator { 2 public Mocha(Beverage beverage) { 3 this.beverage = beverage; 4 } 5 6 public String getDescription() { 7 return beverage.getDescription() + ", Mocha"; 8 } 9 10 public double cost() { 11 return .20 + beverage.cost(); 12 } 13 }
Milk.java:
1 public class Milk extends CondimentDecorator { 2 public Milk(Beverage beverage) { 3 this.beverage = beverage; 4 } 5 6 public String getDescription() { 7 return beverage.getDescription() + ", Milk"; 8 } 9 10 public double cost() { 11 return .10 + beverage.cost(); 12 } 13 }
Soy.java:
1 public class Soy extends CondimentDecorator { 2 public Soy(Beverage beverage) { 3 this.beverage = beverage; 4 } 5 6 public String getDescription() { 7 return beverage.getDescription() + ", Soy"; 8 } 9 10 public double cost() { 11 return .15 + beverage.cost(); 12 } 13 }
Whip.java:
1 public class Whip extends CondimentDecorator { 2 public Whip(Beverage beverage) { 3 this.beverage = beverage; 4 } 5 6 public String getDescription() { 7 return beverage.getDescription() + ", Whip"; 8 } 9 10 public double cost() { 11 return .10 + beverage.cost(); 12 } 13 }
测试代码:
Main.java:
1 public class Main { 2 3 public static void main(String[] args) { 4 Beverage beverage = new Espresso(); 5 System.out.println(beverage.getDescription() 6 + " $" + beverage.cost()); 7 8 Beverage beverage2 = new DarkRoast(); 9 beverage2 = new Mocha(beverage2); 10 beverage2 = new Mocha(beverage2); 11 beverage2 = new Whip(beverage2); 12 System.out.println(beverage2.getDescription() 13 + " $" + beverage2.cost()); 14 15 Beverage beverage3 = new HouseBlend(); 16 beverage3 = new Soy(beverage3); 17 beverage3 = new Mocha(beverage3); 18 beverage3 = new Whip(beverage3); 19 System.out.println(beverage3.getDescription() 20 + " $" + beverage3.cost()); 21 } 22 }
结果: