Builder模式

简单的JavaBean 类 来说明 必要参数与可选参数,
参考来自 Glide,Fresco 等调用模式

public class SuperMan {

private String air;
private String food;
private String water;

private String car;
private String money;
private String work;

private SuperMan(Builder build) {
    this.air = build.air;
    this.food = build.food;
    this.water = build.water;


    this.car = build.car;
    this.money = build.money;
    this.work = build.work;
}

public static class Builder {

    private String air;
    private String food;
    private String water;

    private String car;
    private String money;
    private String work;

    public Builder(String air, String food, String water) {
        this.air = air;
        this.food = food;
        this.water = water;
    }

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

    public Builder car(String car) {
        return this;
    }

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

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

public class Demo {
    private void createSuperMan() {

        SuperMan p = new SuperMan.Builder("氧气", "鸡腿", "矿泉水").car("BWM").build();

    }
}

}

你可能感兴趣的:(Builder模式)