建造者(Builder)模式

建造者模式分为两种,一种为经典建造者模式,另一种为变种建造者模式。我们来挨个看下:

一、经典Builder模式

我们先来看下经典Builder模式,先上UML类图
在这里插入图片描述

  1. 先创建一个Product类
public class Product {
    private String partOne;
    private String partTwo;

    public String getPartOne() {
        return partOne;
    }

    public void setPartOne(String partOne) {
        this.partOne = partOne;
    }

    public String getPartTwo() {
        return partTwo;
    }

    public void setPartTwo(String partTwo) {
        this.partTwo = partTwo;
    }

    @Override
    public String toString() {
        return "Product{" +
                "partOne='" + partOne + '\'' +
                ", partTwo='" + partTwo + '\'' +
                '}';
    }
}

2.接着创建Builder接口

public interface Builder {
     void buildPartOne();
     void buildPartTwo();
     Product getProduct();

}

3.实现Builder接口

public class ConcreteBuilderA implements Builder {
    Product mProduct;

    public ConcreteBuilderA() {
        mProduct = new Product();
    }

    @Override
    public void buildPartOne() {
        mProduct.setPartOne("ApartOne");

    }

    @Override
    public void buildPartTwo() {
        mProduct.setPartTwo("ApartTwo");

    }

    @Override
    public Product getProduct() {
        return mProduct;
    }
}
public class ConcreteBuilderB implements Builder {
    Product mProduct;

    public ConcreteBuilderB() {
        mProduct = new Product();
    }

    @Override
    public void buildPartOne() {
        mProduct.setPartOne("BpartOne");

    }

    @Override
    public void buildPartTwo() {
        mProduct.setPartTwo("BpartTwo");

    }

    @Override
    public Product getProduct() {
        return mProduct;
    }
}

4.最后创建Director类

public class Director {
    private Builder mBuilder;

    public Director(Builder builder) {
        mBuilder = builder;
    }
    public void buildProduct(){
        this.mBuilder.buildPartOne();
        this.mBuilder.buildPartTwo();
    }
    public Product getProduct(){
        return this.mBuilder.getProduct();
    }

}

5.调用

  public static void main(String[] args) {
        Director directorA=new Director(new ConcreteBuilderA());
        directorA.buildProduct();
        Product a=directorA.getProduct();
        System.out.println(a);
        System.out.println("=========================华丽的分割线========================");
        Director directorB=new Director(new ConcreteBuilderB());
        directorB.buildProduct();
        Product b=directorB.getProduct();
        System.out.println(b);
    }

二、Builder的变种

1.使用构造方法

public class UserA {
    private final String name;
    private final int age;
    private final String gender;
    private final String phone;
    public UserA(String name, int age) {
        this(name,age,null);
    }
    public UserA(String name, int age, String gender) {
        this(name,age,gender,null);
    }
    public UserA(String name, int age, String gender, String phone) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.phone = phone;
    }
}

2.使用get和set方法

public class UserB {
    private  String name;
    private  int age;
    private  String gender;
    private  String phone;

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getGender() {
        return gender;
    }

    public String getPhone() {
        return phone;
    }

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

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

    public void setGender(String gender) {
        this.gender = gender;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

3.具备上面两种的优点

public class UserC {
    private final String name;
    private final int age;
    private final String gender;
    private final String phone;

    private UserC(Builder builder) {
        name = builder.name;
        age = builder.age;
        gender = builder.gender;
        phone = builder.phone;
    }


    public static final class Builder {
        private String name;
        private int age;
        private String gender;
        private String phone;

        public Builder() {
        }

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

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

        public Builder gender(String val) {
            gender = val;
            return this;
        }

        public Builder phone(String val) {
            phone = val;
            return this;
        }

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

调用代码,很爽

  new UserC.Builder()
                .name("hello")
                .age(1)
                .gender("male")
                .phone("123")
                .build();

Android Studio中可以安装InnerBuilder插件自动生成代码

发布了6 篇原创文章 · 获赞 4 · 访问量 120
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览