Java设计模式 - 创建型模式 - 建造者模式

介绍

建造者模式(Builder Pattern)使用多个简单的对象一步一步构建成一个复杂的对象。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
一个 Builder 类会一步一步构造最终的对象。该 Builder 类是独立于其他对象的。

特点

将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示。主要解决在软件系统中,有时候面临着”一个复杂对象”的创建工作,其通常由各个部分的子对象用一定的算法构成;由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将它们组合在一起的算法却相对稳定。

角色

  • Product:表示被构造的复杂对象。
  • builder:为创建一个产品对象的各个部件指定抽象接口。
  • ConcreteBuilder:实现Builder的接口以构造和装配该产品的各个部件,定义并明确它所创建的表示,并提供一个检索产品的接口。
  • Director:构造一个使用Builder接口的对象。

优缺点

优点:

  • 建造者独立,易扩展;
  • 便于控制细节风险。

缺点:

  • 产品必须有共同点,范围有限制。

  • 如内部变化复杂,会有很多的建造类。

使用场景

  1. 需要生成的对象具有复杂的内部结构。
  2. 需要生成的对象内部属性本身相互依赖。

注意事项:与工厂模式的区别是:建造者模式更加关注与零件装配的顺序。

举例

创建Product

/**
 * 具体的产品 Product
 * Created by ZhangYW on 2018/4/20.
 */

public class Person {
    private String head;
    private String body;
    private String foot;

    public String getHead() {
        return head;
    }

    public void setHead(String head) {
        this.head = head;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getFoot() {
        return foot;
    }

    public void setFoot(String foot) {
        this.foot = foot;
    }
}

创建builder

/**
 * 抽象建造 builder
 * Created by ZhangYW on 2018/4/20.
 */

public interface Builder {
    void buildHead();
    void buildBody();
    void buildFoot();
    Person buildPerson();//组装
}

创建ConcreteBuilder

/**
 * 具体的建造 ConcreteBuilder
 * Created by ZhangYW on 2018/4/20.
 */

public class ConcreteBuilder implements Builder{
    private Person person;

    public ConcreteBuilder(){

        person = new Person();
    }
    @Override
    public void buildHead() {
        person.setHead("建造头部");
    }

    @Override
    public void buildBody() {
        person.setBody("建造身体");
    }

    @Override
    public void buildFoot() {
        person.setFoot("建造四肢");
    }

    @Override
    public Person buildPerson() {
        return person;
    }
}

创建Director

/**
 * 导演者 Director
 * Created by ZhangYW on 2018/4/20.
 */

public class Director {
    public Person constructPerson(Builder builder) {
        builder.buildHead();
        builder.buildBody();
        builder.buildFoot();
        return builder.buildPerson();
    }
}

测试类

/**
 * 测试类
 * Created by ZhangYW on 2018/4/20.
 */

public class Test {
    public static void main(String[] args) {
        Director director = new Director();
        Person person = director.constructPerson(new ConcreteBuilder());
        System.out.println(person.getHead());
        System.out.println(person.getBody());
        System.out.println(person.getFoot());
    }
}

上面是标准的建造者模式,不怎么好理解,下面举个常见的:
创建Product

public class Person {
    private String name;
    private int age;

    public Person(String name,int age) {
        this.name = name;
        this.age = age;
    }

}

创建Builder

public class Builder {
    private String name;
    private int age;

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

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

    public Person create() {
        return new Person(name, age);
    }
}

测试类

public class Test {
    public static void main(String[] args) {

        Builder builder = new Builder();
        builder.setName("zhangsan");
        builder.setAge(20);
        Perons person = builder.create();
    }
}

还有更清晰的一种:

/**
 * Created by ZhangYW on 2018/4/20.
 */

public class MyPerson {
    private String name;
    private int age;
    private double height;
    private double weight;

    private MyPerson(Builder builder) {
        this.name=builder.name;
        this.age=builder.age;
        this.height=builder.height;
        this.weight=builder.weight;
    }
    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    static class Builder{
        private String name;
        private int age;
        private double height;
        private double weight;
        public Builder name(String name){
            this.name=name;
            return this;
        }
        public Builder age(int age){
            this.age=age;
            return this;
        }
        public Builder height(double height){
            this.height=height;
            return this;
        }

        public Builder weight(double weight){
            this.weight=weight;
            return this;
        }

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

测试类

/**
 * 测试类
 * Created by ZhangYW on 2018/4/20.
 */

public class Test {

        MyPerson.Builder builder = new MyPerson.Builder();
        MyPerson myPerson = builder
                .name("zhangsan")
                .age(20)
                .build();
    }
}

补充:
android中有很多地方用到了建造者模式,例如AlertDialog、Notification、第三方网络请求框架okhttp等

你可能感兴趣的:(#,java基础,设计模式,java,设计模式,创建型模式,建造者模式)