实现茶水间:茶可以分红茶和绿茶,每种茶又可以分大杯和中杯,现在你是服务员需要计算茶水的价格。
package Bridge;
public class BlackTea implements TeaKind{
private float redTeaPrice = 2.0f;
@Override
public float price() {
return redTeaPrice;
}
}
package Bridge;
public class GreenTea implements TeaKind{
private float greenTeaPrice = 3.0f;
@Override
public float price() {
return greenTeaPrice;
}
}
package Bridge;
public class MediumSize implements TeaSize{
TeaKind teaKind;
public MediumSize(TeaKind teaKind) {
this.teaKind = teaKind;
}
@Override
public float getPrice() {
return teaKind.price()*2;
}
}
package Bridge;
public class SuperSize implements TeaSize{
private TeaKind teaKind;
public SuperSize(TeaKind teaKind) {
this.teaKind = teaKind;
}
@Override
public float getPrice() {
return teaKind.price()*3;
}
}
package Bridge;
public interface TeaKind {
public abstract float price();
}
package Bridge;
public interface TeaSize {
public abstract float getPrice();
}
package Bridge;
public class Client {
public static void main(String[] args) {
TeaSize tea1 = new MediumSize(new GreenTea());
System.out.println("绿茶中杯的价格:" + tea1.getPrice());
TeaSize tea2 = new SuperSize(new GreenTea());
System.out.println("绿茶大杯的价格:" + tea2.getPrice());
TeaSize tea3 = new MediumSize(new BlackTea());
System.out.println("红茶中杯的价格:" + tea3.getPrice());
TeaSize tea4 = new SuperSize(new BlackTea());
System.out.println("红茶大杯的价格:" + tea4.getPrice());
}
}