题目
一个咖啡店可以提供大杯(JorumCoffee)、中杯(MediumCoffee)、小杯(SmallCoffee)的咖啡(Coffee),为了满足不同用户的口味,在咖啡中可以添加牛奶(Milk),或者糖(Sugar),或者柠檬(Lemon),提供给用户不同口味的组合,如大杯咖啡加牛奶,中杯咖啡加糖,小杯咖啡加柠檬,小杯咖啡加糖等。
代码
定义口味接口,及咖啡接口
口味
interface Additives{
public void add();
}
咖啡
interface Coffee{
public void add(Additives additives);
public void drink();
}
实现类
口味
class Milk implements Additives{
@Override
public void add() {
System.out.println("牛奶");
}
}
class Sugar implements Additives{
@Override
public void add() {
System.out.println("糖");
}
}
class Lemon implements Additives{
@Override
public void add() {
System.out.println("柠檬");
}
}
咖啡
class smallCoffee implements Coffee{
private Additives additives;
@Override
public void add(Additives additives) {
this.additives=additives;
}
@Override
public void drink() {
additives.add();
System.out.println("小咖啡");
}
}
class mediumCoffee implements Coffee{
private Additives additives;
@Override
public void add(Additives additives) {
this.additives=additives;
}
@Override
public void drink() {
additives.add();
System.out.println("中咖啡");
}
}
class bigCoffee implements Coffee{
private Additives additives;
@Override
public void add(Additives additives) {
this.additives=additives;
}
@Override
public void drink() {
additives.add();
System.out.println("大咖啡");
}
}