首页 > 其他 > 详细

Builder设计模式

时间:2015-12-18 02:10:28      阅读:201      评论:0      收藏:0      [点我收藏+]
public class BuilderPattern {
	private String name ;
	private int age;
	private String email;
	BuilderPattern(Builder builder){
		this.name = builder.name;
		this.age = builder.age;
		this.email = builder.email;
	}
	public void printinfo(){
		System.out.println("name:"+this.name+"-----age:"+this.age+"----email:"+this.email);
	}
	public static void main(String[] args) {
		BuilderPattern bp = new BuilderPattern.Builder().setName("javaeye").setAge(23).setEamil("aa@qq.com").build(); 
		bp.printinfo();
	}
	private static class Builder{
		private String name ;
		private int age;
		private String email;
		public BuilderPattern build(){
			return new BuilderPattern(this);
		}
		public Builder(){
		}
		public Builder setName(String name){
			this.name = name;
			return this;
		}
		public Builder setAge(int age){
			this.age = age;
			return this;
		}
		public Builder setEamil(String email){
			this.email = email;
			return this;
		}
	}
}

?这种方式适合对于复杂的对象,简单的调用方式,避免了一行一行的setXxxx或者是 糊里糊涂的构造

Builder设计模式

原文:http://maclab.iteye.com/blog/2264759

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!