本文主要是介绍Builder模式的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
概念
在创建复杂对象时,将创建该对象的工作交给一个建造者,这个建造者就是一个Builder。在日常的开发中,常常看到,如下这些代码:
AlertDialog的实现
AlertDialog.Builder builder = new AlertDialog.Builder(context);builder.setMessage("你好建造者");builder.setTitle("提示");AlertDialog dialog = builder.create();dialog.show();
其实你会发现,这个Builder是否多余呢?有时候是这么想的,可是如果你将这个代码进行如下写法,你就发现了buidler在一定程度上是优雅了代码:
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);builder1.setMessage("你好建造者").setTitle("提示").setPositiveButton("yes", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {}}).setNegativeButton("no", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {}});AlertDialog dialog1 = builder1.create();dialog1.show();
发现builder在创建对象的同时不停得返回对象本身this,这样代码就变成了一种链式的写法,很优雅。
实践
又一次在对于使用高德地图进行搜索时,我无意间使用了这个模式,感觉很漂亮的。流程如下:
* 请求的路径需要进行参数输入,而且参数相对多如: http://restapi.amap.com/v3/place/text?key=461f8bfebc9395589bb5e8ea28237c79&keywords=菜市场&city=梅州市&citylimit=true&offset=20&page=1&output=json
* 如直接使用String+String的话,感觉代码很不清晰
基于上面两点我写了这样的代码,虽然不是很规范的Builder模式,但是看起来还不错的。
URLBuidler路径的建造者
package com.jsoup.caishichang;import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;/*** Created by owant on 16/2/27.*/
public class URLBuilder {private volatile StringBuffer buffer;private volatile String cityName;private volatile String pageNumber;public URLBuilder() {buffer = new StringBuffer("http://restapi.amap.com/v3/place/text?key=461f8bfebc9395589bb5e8ea28237c79&citylimit=true&offset=20&output=json");}/*** 设置城市名字** @param city_code* @return*/public URLBuilder setCityCode(String city_code) {buffer.append("&city=").append(city_code);this.cityName = city_code;return this;}/*** 设置页数** @param page* @return*/public URLBuilder setPage(String page) {buffer.append("&page=").append(page);this.pageNumber = page;return this;}public URLBuilder setSearchKeyWorld(String keyWorld) throws UnsupportedEncodingException {String words_key_value = URLEncoder.encode(keyWorld, "UTF-8");buffer.append("&keywords=").append(words_key_value);return this;}public String getValue() {return buffer.toString();}public String getCityName() {return cityName;}public String getPageNumber() {return pageNumber;}
}
Client使用情况
package com.jsoup.caishichang;import java.io.UnsupportedEncodingException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;/*** Created by owant on 2/26/16.*/
public class Client {//query=阳江市&city=441702
// private final static String cityCode = "阳江市";private final static String cityCode = "梅州市";//query=江门市&city=440703private final static String cityCode_2 = "江门市";/*** 阳江市* <p>* 1843,江城区,211* 1844,阳西县,211* 1845,阳东县,211* 1846,阳春市,211*/public static void main(String[] arg) throws UnsupportedEncodingException {//打印器Print print = Print.getInstance();//阳江市 12yueExecutorService threadPool = Executors.newFixedThreadPool(8);for (int i = 1; i <= 66; i++) {//路径创建URLBuilder builder = new URLBuilder();builder.setCityCode(cityCode).setSearchKeyWorld("菜市场").setPage(i + "");//添加进线程池threadPool.execute(new CaishiChangRun(builder, print));}threadPool.shutdown();//线程//new Thread(new CaishiChangRun(builder, print)).start();}}
总结
- 对于Builder模式的使用关键是巧妙使用return this;
- 对于Builder一般在其创建对象的内部,是一个静态内部类;
- 创建复杂度高的对象时使用Builder;
这篇关于Builder模式的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!