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

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

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

1.来个手机接口 

public interface Phone {
    void show();
}

2.我手机的参数拿出来. 

public class SeniorParams {
    protected String telephone;
    protected String sim;

    protected void start() {
    }

    protected void end() {
    }
}

3.来个高级华为手机,实现手机的接口,把参数包裹放进去 

public class SeniorHuaWeiPhone implements Phone{
    private SeniorParams mSeniorParams;

    private SeniorHuaWeiPhone(Builder builder) {
        mSeniorParams = builder.mSeniorParams;
    }

    @Override
    public void show() {
    }


    public static class Builder {
        private SeniorParams mSeniorParams;

        public Builder() {
            mSeniorParams = new SeniorParams();
        }

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

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

        public Builder start() {
            this.mSeniorParams.start();
            return this;
        }

        public Builder end() {
            this.mSeniorParams.end();
            return this;
        }

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

 

相关标签: 23种设计模式