欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

23种设计模式----Buidler模式1

程序员文章站 2022-06-04 23:16:25
...
Buidler模式1
将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示.

1. 华为手机, 里面来个builder类,通过builder类的进行对象赋值,最后公狗builder类的build方法,返回当前类.

public class HuaWeiPhone {
    private String telephone;
    private String sim;
    private String application;
    private String framwork;

    private HuaWeiPhone(Builder builder) {
        this.telephone = builder.telephone;
        this.sim = builder.sim;
        this.application = builder.application;
        this.framwork = builder.framwork;
    }


    public static  class Builder {
        private String telephone;
        private String sim;
        private String application;
        private String framwork;

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

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

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

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

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

2.来个测试类 

public class Test {
    public void test() {
        HuaWeiPhone huaWeiPhone = new HuaWeiPhone.Builder().application("").framwork("").sim("").telephone("").build();
    }
}

 

相关标签: 23种设计模式