1. 程式人生 > >Builder模式兩種常見寫法

Builder模式兩種常見寫法

      Builder模式是23中常見模式的一種,最近做Android SDK開發中使用Builder模式比較多一點,自己寫了一個簡單示例demo,在這裡mark一下,如有寫的不對的地方還請大家指正!

/**
 * A simple demo of builder pattern
 * @author Ricky Feng
 *
 */
public class LruCache {
	private final int initialSize;
	private final int maxSize;
	private final int coreThreadNum;
	private final File cacheDir;
	
	public LruCache(Builder builder) {
		this.initialSize = builder.initialSize;
		this.maxSize = builder.maxSize;
		this.coreThreadNum = builder.coreThreadNum;
		this.cacheDir = builder.cacheDir;
	}
	
	public int getInitialSize() {
		return initialSize;
	}
	public int getMaxSize() {
		return maxSize;
	}
	public int getCoreThreadNum() {
		return coreThreadNum;
	}
	public File getCacheDir() {
		return cacheDir;
	}

	public static class Builder {
		private int initialSize;
		private int maxSize;
		private int coreThreadNum;
		private File cacheDir;
		
		public Builder initialSize(int initialSize) {
			this.initialSize = initialSize;
			return this;
		}
		public Builder maxSize(int maxSize) {
			this.maxSize = maxSize;
			return this;
		}
		public Builder coreThreadNum(int coreThreadNum) {
			this.coreThreadNum = coreThreadNum;
			return this;
		}
		public Builder cacheDir(File cacheDir) {
			this.cacheDir = cacheDir;
			return this;
		}
		
		public LruCache build() {
			
			initDefaultValue(this);
			
			return new LruCache(this);
		}
		
		private void initDefaultValue(Builder builder) {
			
			if(builder.initialSize<1){
				builder.initialSize = 1024;
			}
			
			if(builder.maxSize<1){
				builder.maxSize = 1024;
			}
			
			if(builder.coreThreadNum<1){
				builder.coreThreadNum = 3;
			}
			
			if(builder.cacheDir==null){
				builder.cacheDir = new File("./");
			}
		}
	}

	@Override
	public String toString() {
		return "LruCache [initialSize=" + initialSize + ", maxSize=" + maxSize
				+ ", coreThreadNum=" + coreThreadNum + ", cacheDir=" + cacheDir
				+ "]";
	}

}


客戶端呼叫程式碼如下

LruCache cache = new LruCache.Builder().initialSize(1024 * 16)
				.maxSize(1024 * 64).coreThreadNum(8).build();

		System.out.println(cache);


       個人覺得理解常用的23種設計模式還是非常有必要的,畢竟這些都是前輩們思想的結晶,可以優雅的解決開發中的需要問題。但是設計不是萬能的,切勿為了使用設計模式而去設計,避免濫用設計模式!

-------------------------------------------------------------------------------------------

最近在看HttpClient 4.3 原始碼的時候發現了一種更直觀的寫法,具體可以參考 org.apache.http.client.config.RequestConfig類,自己依葫蘆畫瓢寫了一個簡單的示例,mark一下!

/**
 * builder pattern
 * @author Ricky Feng
 * @version 2015-02-27 10:42
 */
public class IvrConfig {
	private final String apiKey;
	private final String secret;
	private final int interval;
	private final File ivrFileDir;
	
	IvrConfig(final String apiKey,final String secret,final int interval,final File ivrFileDir){
		this.apiKey = apiKey;
		this.secret = secret;
		this.interval = interval;
		this.ivrFileDir = ivrFileDir;
	}
	
	public String getApiKey() {
		return apiKey;
	}

	public String getSecret() {
		return secret;
	}

	public int getInterval() {
		return interval;
	}

	public File getIvrFileDir() {
		return ivrFileDir;
	}
	
	/**create Builder method**/
	public static IvrConfig.Builder custom() {
		return new Builder();
	}
	
	@Override
	public String toString() {
		return "IvrConfig [apiKey=" + apiKey + ", secret=" + secret
				+ ", interval=" + interval + ", ivrFileDir=" + ivrFileDir + "]";
	}

	
	public static class Builder {
		private String apiKey;
		private String secret;
		private int interval;
		private File ivrFileDir;
		
		Builder(){
			this.apiKey = "null";
			this.secret = "null";
			this.interval = -1;
			this.ivrFileDir = new File("./");
		}
		
		public Builder setApiKey(final String apiKey) {
			this.apiKey = apiKey;
			return this;
		}
		
		public Builder setSecret(final String secret) {
			this.secret = secret;
			return this;
		}
		
		public Builder setinterval(final int interval) {
			this.interval = interval;
			return this;
		}
		
		public Builder setIvrFileDir(final File ivrFileDir) {
			this.ivrFileDir = ivrFileDir;
			return this;
		}
		
		public IvrConfig build(){
			
			return new IvrConfig(apiKey, secret, interval, ivrFileDir);
		}
	}
	
}


客戶端呼叫程式碼如下:

IvrConfig config = IvrConfig.custom().setApiKey("abc").setSecret("bb").build();
		
		System.out.println(config);