美文网首页
23种设计模式-构建者模式(Builder)

23种设计模式-构建者模式(Builder)

作者: 王灵 | 来源:发表于2019-03-02 22:09 被阅读0次

作用:帮着构建复杂对象;(如:自定义view)

也称为Builder 模式,可能没写过但一定都见过。
首先是一个非常复杂的对象,虽然构造方法很简单但是它的使用很灵活有很多可配置项。这个时候我们new一个然后看源码一项项去配置明显需要很多功夫。有没有一个方式可以让我们只需要关注到它可以配置的项呢。有,构建者。帮着我们创建复杂的对象。

构建的三种方式:

针对Product类构建

public class Product {
    private int id;
    private String name;
    private int type;
    private float price;
}
  • 构造器重载
public class Product {
    private int id;
    private String name;
    private int type;
    private float price;

    public Product(int id) {
        this.id = id;
    }
。。。

    public Product(int id, String name) {
        this.id = id;
        this.name = name;
    }
。。。

    public Product(int id, String name, int type) {
        this.id = id;
        this.name = name;
        this.type = type;
    }
。。。

    public Product(int id, String name, int type, float price) {
        this.id = id;
        this.name = name;
        this.type = type;
        this.price = price;
    }
}

理论上这种构建方式我们需要生成xxxx非常多的构造方法

  • javaBeans方式
public class Product {
    private int id;
    private String name;
    private int type;
    private float price;

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setType(int type) {
        this.type = type;
    }

    public void setPrice(float price) {
        this.price = price;
    }
}

相比简单了很多,但是,因为构造过程被分到了几个调用中,在构造过程中JavaBeans可能处于不一致的状态,类无法仅仅通过检验构造器参数的有效性来保证一致性。

  • builder模式
 public class Product {
    private int id;
    private String name;
    private int type;
    private float price;

    public Product(Builder builder) {
        this.id = builder.id;
        this.name = builder.name;
        this.type = builder.type;
        this.price = builder.price;
    }

    public static class Builder {
        private int id;
        private String name;
        private int type;
        private float price;

        public Builder id(int id) {
            this.id = id;
            return this;
        }

        public Builder name(String name) {
            this.name = name;
            return this;
        }

        public Builder type(int type) {
            this.type = type;
            return this;
        }

        public Builder price(float price) {
            this.price = price;
            return this;
        }

        public Product build() {
            return new Product(this);
        }
    }
}
 Product p = new Product.Builder()
                .id(10)
                .name("phone")
                .price(100)
                .type(1)
                .build();

先通过某种方式取得构造对象需要的所有参数,然后通过这些参数一次性构造这个对象

相关文章

  • Mybatis中的设计模式

    Mybatis 设计模式 mybaits最少用到了九种设计模式: 设计模式mybaits体现Builder构建者模...

  • 安卓设计模式-构建者模式

    安卓设计模式-构建者模式 是什么 构建者模式又生builder模式,是将复杂的对象的构建与他的表示分离,使得同样的...

  • 设计模式--Builder

    标签(空格分隔): android 设计模式 builder 1 Builder设计模式概述 将复杂对象的构建与它...

  • Builder模式

    一、作用 工匠若水-设计模式(创建型)之建造者模式(Builder Pattern) 用于构建复杂的对象,将构建的...

  • Android Builder模式笔记

    Builder模式介绍 Builder模式又称建造者模式,表示将构建过程和表示过程进行分离,让(参数)构建过程变得...

  • 【设计模式】- Builder(构建者)模式

    简介 当我们在构建一个复杂的对象时,想要把对象的构建和表现分离开来,那么就可以使用Builder模式。比如:构建一...

  • 设计模式-Builder模式(构建者模式)

    为什么要用Builder模式?什么时候用? Builder模式一般用于构造一个复杂对象时使用,可以屏蔽构造的细节,...

  • 建造者模式(Builder 模式)

    Android进阶之设计模式 建造者模式( Builder 模式) 定义:将一个复杂对象的构建与它的表示分离,使得...

  • 2.创建者模式——Builder

    设计模式——创造者模式(Builder)

  • 46 - 建造者模式

    本文,我们来学习另外一个比较常用的创建型设计模式,Builder 模式,中文翻译为建造者模式或者构建者模式,也有人...

网友评论

      本文标题:23种设计模式-构建者模式(Builder)

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