public class aaa {
public static void main(String[] args) {
Drink coffe = new Coffe();
Drink suger = new Suger(coffe);
System.out.println(suger.info()+"-->"+suger.cost());
}
}
//抽象组件
interface Drink{
double cost();//费用
String info();//说明
}
//具体组件
class Coffe implements Drink{
private String name = "原味咖啡";
@Override
public double cost() {
return 10;
}
@Override
public String info() {
return name;
}
}
//具体装饰类
class Suger implements Drink{
private Drink drink;
public Suger(Drink drink) {
this.drink = drink;
}
@Override
public double cost() {
return this.cost()*2;
}
@Override
public String info() {
return this.info()+"加入了糖";
}
}