java 建造者模式详解

package com.poizon;


import lombok.Data;

/**
 * @Author: wpp
 * @Date: 2021/10/13 17:03
 */
@Data
public class Persion {
    String name;
    String age;

}

建造者类

package com.poizon;


/**
 * @Author: wpp
 * @Date: 2021/10/13 17:05
 */
public class PersionBuilder {

    private  Persion persion;

    public PersionBuilder(){
        this.persion = new Persion();
    }
    public PersionBuilder name(String name){
        this.persion.setName(name);
        return this;
    }

    public PersionBuilder age(String age){
        this.persion.setAge(age);
        return this;
    }

    public Persion builder(){
        return this.persion;
    }

    public static void main(String[] args) {
        PersionBuilder persionBuilder = new PersionBuilder();

        Persion persion  = persionBuilder.age("33").name("21").builder();
        Persion persion2  = persionBuilder.name("21").age("33").builder();

    }
}

你可能感兴趣的:(flink,java)