Builder设计模式Demo

 

作为Java/Android开发者, 如果你想设计一个通用的库, Builder模式几乎肯定是会用到的, 我们需要提供良好的入口/接口来使用者可以弹性地构造他们需要的对象.

像一些优秀的开源库, 例如Glide, OkHttp等都在构造Glide, OkHttpClient时用到Builder模式, 例如OkHttpClient的创建, 如下节选OkHttpClient.java的代码:

 

public class OkHttpClient implements Cloneable, Call.Factory {
    public OkHttpClient() {
        this(new Builder());
    }

    private OkHttpClient(Builder builder) {
        this.dispatcher = builder.dispatcher;
        this.proxy = builder.proxy;
        this.protocols = builder.protocols;
        this.connectionSpecs = builder.connectionSpecs;
        this.interceptors = Util.immutableList(builder.interceptors);
        this.networkInterceptors = Util.immutableList(builder.networkInterceptors);
        this.proxySelector = builder.proxySelector;
        this.cookieJar = builder.cookieJar;
        this.cache = builder.cache;
        this.internalCache = builder.internalCache;
        this.socketFactory = builder.socketFactory;

        // more code...
    }

    public static final class Builder {

        public Builder() {
            ...
        }


        public Builder cache(Cache cache) {
            ...
        }

        public Builder dispatcher(Dispatcher dispatcher) {
            ...
        }

        public Builder protocols(List protocols) {
            ...
        }

        public List networkInterceptors() {
            ...
        }

        public Builder addNetworkInterceptor(Interceptor interceptor) {
            ...
        }

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

 

 

Buidler模式, 是一种创建型的设计模式.

通常用来将一个复杂的对象的构造过程分离, 让使用者可以根据需要选择创建过程.

 

 

另外, 当这个复杂的对象的构造包含很多可选参数时, 那Builder模式可以说是不二之选.

 

所以今天写了一个Builer模式的一个demo  

一个饺子的类  然后顾客根据自己的喜好自助添加作料

 

public class Dumpling {

    private boolean soy;
    private boolean vinegar;
    private boolean garlic;
    private boolean chili;

    public Dumpling(Builder builder) {
        this.soy = builder.soy;
        this.chili = builder.chili;
        this.garlic = builder.garlic;
        this.vinegar = builder.vinegar;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder("来一盘饺子放:");

        if (this.soy){
            builder.append("酱油");
        }
          if (this.chili){
            builder.append("辣椒");
        }
          if (this.vinegar){
            builder.append("醋");
        }
          if (this.garlic){
            builder.append("蒜");
        }

        return builder.toString();
    }

    public static class Builder {
        private boolean soy;
        private boolean vinegar;
        private boolean garlic;
        private boolean chili;

        public Builder() {
        }

        public Builder withSoy() {
            this.soy = true;
            return this;
        }

        public Builder withVinegar() {
            this.vinegar = true;
            return this;
        }

        public Builder withGarlic() {
            this.garlic = true;
            return this;
        }

        public Builder withChili() {
            this.chili = true;
            return this;
        }

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

 

一般来说Builder常常作为静态内部类(提高内聚性)

 

下面是顾客自助活动

 

public class BuilderDemo {
    public static void main(String[] args) {
        Dumpling customer1 = new Dumpling.Builder()
                .withChili()
                .withGarlic()
                .withSoy()
                .build();

        System.out.println("第一个顾客"+customer1.toString());

        Dumpling customer2 = new Dumpling.Builder()
                .withSoy()
                .withVinegar()
                .build();
        System.out.println("第二个顾客"+customer2.toString());

        Dumpling customer3 = new Dumpling.Builder()
                .withVinegar()
                .withChili()
                .withGarlic()
                .build();

        System.out.println("第三个顾客"+customer3.toString());
    }
}

运行结果

 

第一个顾客来一盘饺子放:酱油辣椒蒜
第二个顾客来一盘饺子放:酱油醋
第三个顾客来一盘饺子放:辣椒醋蒜

什么是 Demo Builder 11? Demo Builder 提供了一种简单的方法来创建显示软件和系统是如何工作的教程,演示或示范。Demo Builder 的易用性使您能够创建令人惊叹,但实际与专业教学视频的功能。Demo Builder 用于生成你需要教育,市场,或出售该演示的理想工具。 Demo Builder 11 如何工作? 1,录制屏幕 录制你的桌面应用程序运行时的操作过程,捕获所有的鼠标移动,点击,用户的行动和项目。 2,编辑视频 加入气球,注释及效果,如3D变换,缩放和平移,更增强您的教程。 3,发布 通过 YouTube 发布您的教程或保存为 Flash,独立文件,图像文件和视频文件轻松共享。 Tanida Demo Builder 11 版本 11 版让你的视频上传到 Dropbox,GoogleDrive 和 SkyDrive 的功能。 Flash 视频工具也得到了提高;现在你可以在你的 Demo Builder 影片播放 YouTube 视频。 变焦图片缩放 新的和改进的功能: 上传你的影片到 Dropbox,谷歌驱动器和 SkyDrive 预览 只需一次点击,上传和分享影片到 Dropbox,谷歌驱动器和 SkyDrive。 播放 YouTube 视频 预览 使用 Flash 视频工具,您现在可以在您的演示器电影播放 YouTube 视频。 创建非常好看的演示文稿,演示和教程。 现在,使用 Demo Builder 11 更容易! Demo Builder 是首选的专家创作工具,用于创建专业的,全面的交互应用的模拟,演示和教程。Demo Builder 是如此好用!不像一些其他的工具,以演示器没有复杂的界面和以前没有编程知识是必需的。你将建立和运行,在任何时间创造优越的演示文稿。您的观众一定会喜欢它,你会得到你应得的结果。 Demo Builder 11 进一步扩展和扩展了在以前的版本中提供的功能,使您能够创建一个显示如何软件,系统和流程的工作视听Flash影片。它提供捕捉到一个正在运行的应用程序采取的所有行动,那么它可以编辑和注释,产生互动演示和模拟的能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值