Two common ways of writing builder pattern

Source: Internet
Author: User
Tags static class

Builder mode is one of the common patterns in 23, the recent Android SDK development using the builder mode a little bit more, wrote a simple example demo, here mark, if there is a wrong place also please correct me.

/** * A Simple demo of the builder pattern * @author Ricky Feng * * */public class LruCache {private final int initialsiz
	E
	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 to New LruCache (this); } private void Initdefaultvalue (Builder Builder) {if (builder.initialsize<1) {builder.initialsize = 102
			4;
			} 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 + "]"; }

}



The client invokes the code as follows

LruCache cache = new Lrucache.builder (). InitialSize (1024). MaxSize (
				1024 *). Corethreadnum (8). build ();

		SYSTEM.OUT.PRINTLN (cache);


Personally feel that understanding of the 23 commonly used design patterns are still very necessary, after all, these are the crystallization of the minds of predecessors, can be elegant solution to the needs of development problems. But design is not omnipotent, do not use design patterns to design, avoid misuse of design patterns.



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


Recently in the httpclient 4.3 source code found a more intuitive way of writing, can refer to Org.apache.http.client.config.RequestConfig class, oneself according to gourd painting Scoop wrote a simple example, mark.

/** * Builder pattern * @author Ricky Feng * @version 2015-02-27 10:42/public class Ivrconfig {private Final Stri
	Ng 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);
 }
	}
	
}


The client calls the code as follows:

Ivrconfig config = Ivrconfig.custom (). Setapikey ("abc"). Setsecret ("BB"). Build ();
		
		System.out.println (config);




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.