动态的将责任附加到对象上,若是要扩展功能,装饰者提供了比继承更加具有弹性的替代方案。
其中,装饰者模式秉承着一个原则:对外扩展开放,对修改关闭。、
下面以一个例子为实例来加强对此模式的理解
以下是星巴克咖啡生产咖啡为例,定义一个Beverage abstract class 用于表示饮料的总称,里面包括两个方法getDescription()、cost()。定义一个装饰组件CondimentDecorate CoComponent,其主要是用来调料装饰的。其继承Beverage抽象类。定义四中咖啡类型:DarkRoast、Decaf、Espresso、HouseBlend。四个装饰调料对象:Milk、Mocha、Soy、Whip。其中调料组件和具体的调料组件具有相同的超类属性。我们可以在类动态的运行的时候再根据客户的需求加入相应的调料,从而可以生产各种各样的咖啡饮料。
以下是具体代码实现:
package com.clark.decoratepattern.abstractclass;
/**
* 定义一个饮料抽象类
* @author Administrator
*
*/
public abstract class Beverage {
public String description="Unknown Beverage";
public String getDescription(){
return description;
}
public abstract double cost();
}
package com.clark.decoratepattern.abstractclass;
/**
* 调料抽象类,用于装饰饮料
* 其应该和被装饰的类具有相同的超类
* @author Administrator
* 所有的装饰者类也即调料者都必须重新实现getDescription()方法
*/
public abstract class CondimentDecorate extends Beverage {
public abstract String getDescription();
}
package com.clark.decoratepattern;
import com.clark.decoratepattern.abstractclass.Beverage;
/**
* 综合咖啡类
* @author Administrator
*
*/
public class HouseBlend extends Beverage {
public double cost() {
return 0.89;
}
public HouseBlend(){
description="House Blend Coffee";
}
}
package com.clark.decoratepattern;
import com.clark.decoratepattern.abstractclass.Beverage;
/**
* 浓缩咖啡类
* @author Administrator
*
*/
public class Espresso extends Beverage {
//属性继承自父类
public Espresso(){
description="Espresso";
}
public double cost() {
return 1.99;
}
}
package com.clark.decoratepattern;
import com.clark.decoratepattern.abstractclass.Beverage;
/**
* 低咖啡因
* @author Administrator
*
*/
public class Decaf extends Beverage {
public double cost() {
return 1.05;
}
public Decaf(){
description="decaf";
}
}
package com.clark.decoratepattern;
import com.clark.decoratepattern.abstractclass.Beverage;
/**
* 深度烘焙咖啡
* @author Administrator
*
*/
public class DarkRoast extends Beverage{
@Override
public double cost() {
return 0.99;
}
public DarkRoast(){
description="dark roast coffee";
}
}
package com.clark.decoratepattern.decorate;
import com.clark.decoratepattern.abstractclass.Beverage;
import com.clark.decoratepattern.abstractclass.CondimentDecorate;
/**
* 奶泡Whip调料装饰对象
* @author Administrator
*
*/
public class Milk extends CondimentDecorate {
Beverage beverage;
public Milk(Beverage beverage){
this.beverage=beverage;
}
@Override
public String getDescription() {
return this.beverage.getDescription()+",Milk";
}
@Override
public double cost() {
return this.beverage.cost()+0.10;
}
}
package com.clark.decoratepattern.decorate;
import com.clark.decoratepattern.abstractclass.Beverage;
import com.clark.decoratepattern.abstractclass.CondimentDecorate;
/**
* 摩卡装饰调料类
* @author Administrator
*
*/
public class Mocha extends CondimentDecorate {
Beverage beverage;
public Mocha(Beverage beverage){
this.beverage=beverage;
}
//返回装饰调料的同时把其它的调料也描述
public String getDescription() {
return this.beverage.getDescription()+",Mocha";
}
//返回增加Mocha后的调料总价格
public double cost() {
return 0.20+this.beverage.cost();
}
}
package com.clark.decoratepattern.decorate;
import com.clark.decoratepattern.abstractclass.Beverage;
import com.clark.decoratepattern.abstractclass.CondimentDecorate;
/**
* 豆浆Soy调料的装饰
* @author Administrator
*
*/
public class Soy extends CondimentDecorate {
Beverage beverage;
public Soy(Beverage beverage) {
this.beverage=beverage;
}
@Override
public String getDescription() {
return this.beverage.getDescription()+",Soy";
}
@Override
public double cost() {
return this.beverage.cost()+0.15;
}
}
package com.clark.decoratepattern.decorate;
import com.clark.decoratepattern.abstractclass.Beverage;
import com.clark.decoratepattern.abstractclass.CondimentDecorate;
/**
* 奶泡Whip调料装饰对象
* @author Administrator
*
*/
public class Whip extends CondimentDecorate {
Beverage beverage;
public Whip(Beverage beverage){
this.beverage=beverage;
}
@Override
public String getDescription() {
return this.beverage.getDescription()+",Whip";
}
@Override
public double cost() {
return this.beverage.cost()+0.10;
}
}
以下是测试类:即可以根据客户的需求来生产咖啡
package com.clark.decoratepattern;
import com.clark.decoratepattern.abstractclass.Beverage;
import com.clark.decoratepattern.decorate.Milk;
import com.clark.decoratepattern.decorate.Mocha;
import com.clark.decoratepattern.decorate.Soy;
import com.clark.decoratepattern.decorate.Whip;
/**
* Test class
* @author Administrator
*
*/
public class Test {
public static void main(String[] args) {
//来一杯浓缩咖啡,之家牛奶Milk
Beverage bev=new Espresso();
bev=new Milk(bev);
System.out.println(bev.getDescription()+" $"+bev.cost());
//来一杯深陪咖啡,加牛奶和摩卡
Beverage bev2=new DarkRoast();
bev2=new Milk(bev2);
bev2=new Mocha(bev2);
System.out.println(bev2.getDescription()+" $"+bev2.cost());
//来一杯低咖啡因,内加牛奶、摩卡、奶泡
Beverage bev3=new HouseBlend();
bev3=new Milk(bev3);
bev3=new Mocha(bev3);
bev3=new Whip(bev3);
System.out.println(bev3.getDescription()+" $"+bev3.cost());
//来一杯综合的咖啡,内部加奶牛、摩卡、奶泡、豆浆
Beverage bev4=new Decaf();
bev4=new Milk(bev4);
bev4=new Mocha(bev4);
bev4=new Whip(bev4);
bev4=new Soy(bev4);
System.out.println(bev4.getDescription()+" $"+bev4.cost());
}
}