麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

Builder設計模式Demo

2019-11-09 17:47:14
字體:
來源:轉載
供稿:網友

作為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());    }}運行結果

第一個顧客來一盤餃子放:醬油辣椒蒜第二個顧客來一盤餃子放:醬油醋第三個顧客來一盤餃子放:辣椒醋蒜


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 中文字幕在线永久 | 免费观看视频网站 | 操碰97| 福利在线免费 | 国产精品一区在线看 | 成人av一区二区免费播放 | 草久影视 | 成人在线视频国产 | 一级黄色免费观看 | 欧美激情精品久久久久久久久久 | japanesexxxx24videofree| 久久久一区二区 | 精品无码一区在线观看 | 国产精品免费久久久 | 欧美女人天堂 | 在线成人一区二区 | 国产资源在线免费观看 | 国产午夜精品一区二区三区四区 | 亚洲一区二区在线免费 | 狠狠色成色综合网 | 久久久三区 | 7777视频 | 国产精品爆操 | 龙的两根好大拔不出去h | 国产三级在线视频观看 | av免费在线观看国产 | 天天夜天天操 | 国产一国产精品一级毛片 | 久久色网站 | 激情在线观看视频 | 亚洲天堂在线电影 | 91在线视频观看 | 水卜樱一区二区av | 久久av免费 | av电影免费播放 | 欧美成人午夜影院 | 精品乱码久久久久 | 男女羞羞视频在线免费观看 | 国产精品一区二区免费在线观看 | 亚洲精品午夜电影 | 亚洲日韩中文字幕一区 |