软件设计之桥接模式

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

 

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

相关推荐

  1. 【前端设计模式模式

    2023-12-06 08:10:03       67 阅读
  2. 设计模式模式

    2023-12-06 08:10:03       42 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-06 08:10:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-06 08:10:03       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-06 08:10:03       82 阅读
  4. Python语言-面向对象

    2023-12-06 08:10:03       91 阅读

热门阅读

  1. 使用 Apache Kafka 进行实时流处理

    2023-12-06 08:10:03       48 阅读
  2. HarmonyOS学习--TypeScript语言学习(三)

    2023-12-06 08:10:03       53 阅读
  3. 【无标题】

    2023-12-06 08:10:03       67 阅读
  4. 前端并发多个请求并失败重发

    2023-12-06 08:10:03       86 阅读
  5. 力扣215. 数组中的第K个最大元素

    2023-12-06 08:10:03       66 阅读
  6. 前端】全局替换(replace//g)

    2023-12-06 08:10:03       52 阅读
  7. 前端React基础面试题

    2023-12-06 08:10:03       57 阅读
  8. Debian 终端Shell命令行长路径改为短路径

    2023-12-06 08:10:03       64 阅读