美文网首页
建造者设计模式

建造者设计模式

作者: 不服输的小蜗牛 | 来源:发表于2020-07-26 20:50 被阅读0次

1.什么是建造者设计模式:
使用多个简单的对象一步一步构建成一个复杂的对象。
例如我们想要点一杯奶茶,里面可以添加椰果、珍珠、西米露、烧仙草、龟苓膏、芋圆、红豆,通过一个个添加最终实现我们要的奶茶
2.什么时候使用建造者设计模式:
一些基本部件不会变,而其组合经常变化的时候。简单理解就是在我们开发中一个类的参数超过四个以上,而且有些参数不是必须的。这时我们就可以使用建造者设计模式。建造者模式在开发中很常见,只要最后是以build()方法创建对象的几乎都是建造者设计模式
3建造者设计模式的优缺点:
优点:1、建造者独立,易扩展。 2、便于控制细节风险。
缺点:1、产品必须有共同点,范围有限制。 2、如内部变化复杂,会有很多的建造类。
4建造者模式的实现:
我们以奶茶为例

public class MilkTea {
    private boolean coconut;
    private boolean pearl;
    private boolean sago;
    private boolean burningGrass;
    private boolean guilingPaste;
    private boolean taro;
    private boolean redBeans;

    private MilkTea(Builder builder) {
        this.coconut = builder.coconut;
        this.pearl = builder.pearl;
        this.sago = builder.sago;
        this.burningGrass = builder.burningGrass;
        this.guilingPaste = builder.guilingPaste;
        this.taro = builder.taro;
        this.redBeans = builder.redBeans;
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("您的奶茶中有");
        if(coconut){
            stringBuilder.append("、椰果");
        }
        if(pearl){
            stringBuilder.append("、珍珠");
        }
        if(sago){
            stringBuilder.append("、西米露");
        }
        if(burningGrass){
            stringBuilder.append("、烧仙草");
        }

        if(guilingPaste){
            stringBuilder.append("、龟苓膏");
        }

        if(taro){
            stringBuilder.append("、芋圆");
        }

        if(redBeans){
            stringBuilder.append("、红豆");
        }

        return stringBuilder.toString();
    }

    public static class Builder {
        public boolean coconut;
        public boolean pearl;
        public boolean sago;
        public boolean burningGrass;
        public boolean guilingPaste;
        public boolean taro;
        public boolean redBeans;

        public Builder setCoconut(boolean coconut) {
            this.coconut = coconut;
            return this;
        }

        public Builder setPearl(boolean pearl) {
            this.pearl = pearl;
            return this;
        }

        public Builder setSago(boolean sago) {
            this.sago = sago;
            return this;
        }

        public Builder setBurningGrass(boolean burningGrass) {
            this.burningGrass = burningGrass;
            return this;
        }

        public Builder setGuilingPaste(boolean guilingPaste) {
            this.guilingPaste = guilingPaste;
            return this;
        }

        public Builder setTaro(boolean taro) {
            this.taro = taro;
            return this;
        }

        public Builder setRedBeans(boolean redBeans) {
            this.redBeans = redBeans;
            return this;
        }

        public MilkTea build() {
            return new MilkTea(this);
        }

    }
}

测试


public class Main {
    public static void main(String[] args) {
        MilkTea milkTea = new MilkTea.Builder()
                .setBurningGrass(true)
                .setCoconut(true)
                .setRedBeans(true)
                .build();
        System.out.println(milkTea.toString());
}

输出

您的奶茶中有、椰果、烧仙草、红豆

相关文章

  • 设计模式之建造者模式

    设计模式之建造者模式 Intro 简介 建造者模式: 建造者模式隐藏了复杂对象的创建过程,它把复杂对象的创建过程加...

  • Retrofit

    Retrofit设计模式 动态代理,装饰模式,建造者模式,抽象工厂模式,适配器模式 建造者模式创建Retrofit...

  • java设计模式--建造者模式

    java设计模式--建造者模式 建造者模式定义 建造者模式:是将复杂对象的构建与表示进行分离,使同样的构建过程会有...

  • 建造者设计模式-Builder design pattern

    建造者设计模式是创建型设计模式的一种。创建型设计模式处理对象创建的问题。 建造者设计模式,用来构建需要经过若干个建...

  • 【Java设计模式】--建造者模式

    建造者模式(Bulider模式) 一、什么是建造者模式 建造者模式是设计模式的一种,将一个复杂对象的构建与它的表示...

  • Android中涉及的模式

    我的Java设计模式-建造者模式 我的Java设计模式-观察者模式 重学设计模式之单例模式

  • 设计模式之建造者模式

    设计模式之建造者模式 1. 模式定义 建造者模式又可以成为生成器模式,它属于对象创建型模式。建造者模式将一个复杂对...

  • 深圳7000+

    2019年的目标,努力 1.设计模式-建造者模式(抽象建造者,具体建造者,创建对象,指挥者),优点,缺点,使用场景...

  • 设计模式 - 目录

    设计模式01 - 单例模式 设计模式02 - 工厂模式 设计模式03 - 建造者模式 设计模式04 - 适配器模式...

  • 设计模式-构建者模式

    概念 建造者模式(英:Builder Pattern)是一种创建型设计模式,又名:生成器模式。GOF 给建造者模式...

网友评论

      本文标题:建造者设计模式

      本文链接:https://www.haomeiwen.com/subject/cheglktx.html