首页 > 代码库 > 装饰者模式

装饰者模式

需求:顾客能在购买咖啡(咖啡种类有多种)的同时购买任意种类和数量的调料。

例如顾客想要购买摩卡和奶泡深焙咖啡。分解为:

1.深焙咖啡

2.用调料摩卡装饰深焙咖啡

3.用调料奶泡装饰摩卡深焙咖啡

规定店中所有提供的饮料均继承基类Beverage。

实现图如下:

实现代码如下:

Beverage基类:

public abstract class Beverage {
String description="null description";
double cost;
public String getDescription()
{
return description;
}

public abstract double cost();


public void setCost(double cost) {
this.cost = cost;
}

}

咖啡DarkRoast类:

public class DarkRoast extends Beverage{
//private double cost;

public DarkRoast()
{
description="DarkRoast";
cost=0.99;
}
public DarkRoast(double cost)
{
this.cost=cost;
}
@Override
public double cost() {
// TODO Auto-generated method stub
return cost;
}

public void setCost(double cost) {
this.cost = cost;
}

}

咖啡Decaf类:

public class Decaf extends Beverage{
//private double cost;
public Decaf(){
description="Decaf";
cost=0.9;
}
public Decaf(double cost)
{
this.cost=cost;
}
@Override
public double cost() {
// TODO Auto-generated method stub
return cost;
}

public void setCost(double cost) {
this.cost = cost;
}

}

咖啡Espresso类:

public class Espresso extends Beverage{
//private double cost;
public Espresso(){
description="Espresso";
cost=1.99;
}
public Espresso(double cost)
{
this.cost=cost;
}
@Override
public double cost() {
// TODO Auto-generated method stub
return cost;
}

public void setCost(double cost) {
this.cost = cost;
}

}

咖啡HouseBlend类:

public class HouseBlend extends Beverage{
//private double cost;
public HouseBlend(){
description="HouseBlend";
cost=1.2;
}
public HouseBlend(double cost)
{
this.cost=cost;
}
@Override
public double cost() {
// TODO Auto-generated method stub
return cost;
}

public void setCost(double cost) {
this.cost = cost;
}

}

装饰者CondimentDecorator基类:

public abstract class CondimentDecorator extends Beverage{
public abstract String getDescription();
}

调料Milk类:

public class Milk extends CondimentDecorator{
Beverage beverage;
public Milk(Beverage beverage)
{
this.beverage=beverage;
cost=0.3;
}
@Override
public String getDescription() {
// TODO Auto-generated method stub
return beverage.getDescription()+", Milk";
}

@Override
public double cost() {
// TODO Auto-generated method stub
return cost+beverage.cost();
}
public Beverage getBeverage() {
return beverage;
}
public void setBeverage(Beverage beverage) {
this.beverage = beverage;
}

}

调料Mocha类:

public class Mocha extends CondimentDecorator{
Beverage beverage;
public Mocha(Beverage beverage)
{
this.beverage=beverage;
cost=0.2;
}
@Override
public String getDescription() {
// TODO Auto-generated method stub
return beverage.getDescription()+", Mocha";
}

@Override
public double cost() {
// TODO Auto-generated method stub
return cost+beverage.cost();
}
public Beverage getBeverage() {
return beverage;
}
public void setBeverage(Beverage beverage) {
this.beverage = beverage;
}

}

星兹巴咖啡店模拟:

public class StarbuzzCoffee {
public static void main(String [] args)
{
Beverage beverage=new Espresso();
beverage.setCost(1.999);
System.out.println(beverage.getDescription()+" $"+beverage.cost());

Beverage beverage2=new DarkRoast();
beverage2=new Mocha(beverage2);
beverage2=new Mocha(beverage2);
beverage2=new Milk(beverage2);
System.out.println(beverage2.getDescription()+" $"+beverage2.cost());

}
}