软件设计之桥接模式

实现茶水间:茶可以分红茶和绿茶,每种茶又可以分大杯和中杯,现在你是服务员需要计算茶水的价格。

 

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());
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ོ栖落

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值