论坛首页 Java企业应用论坛

关于 《effective Java》中的建造者模式

浏览 8873 次
精华帖 (0) :: 良好帖 (3) :: 新手帖 (1) :: 隐藏帖 (4)
作者 正文
   发表时间:2012-03-05  

public class Goods {

	private Integer id;
	private String name;
	private String prodAddress;
	private String prodFactory;
	private String description;
	
	
	
	
	public  class GoodBuilder{
		private Integer id;
		private String name;
		private String prodAddress;
		private String prodFactory;
		private String description;
		
		
		public GoodBuilder buildId(Integer id){
			this.id = id;
			return this;
		}
		
		public GoodBuilder buildName(String name){
			this.name = name;
			return this;
		}
		
		public GoodBuilder buildProdAddress(String address){
			this.prodAddress = address;
			return this;
		}
		
		public GoodBuilder builderDescription(String description){
			this.description = description;
			return this;
		}
		
		public GoodBuilder builderFactory(String factory){
			this.prodFactory = factory;
			return this;
		}
		
		public Goods  build(){
			Goods g = new Goods();
			g.id = this.id;
			g.name =  this.name;
			g.prodAddress = this.prodAddress;
			g.description = this.description;
			g.prodFactory = this.prodFactory;
			return g;
		}
	}
}



public class TestBuilder {

	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Goods g = new Goods().new GoodBuilder().buildId(10).builderDescription("test").build();
	}

}


希望大家发表一下看法。   在其他地方讲解的建造者模式于这个有点区别。  其他的地方将来将去都是怎么把多个零件装到一个List中。 本质上都是构件对象,但是在实现方式上有这么大的区别?

   发表时间:2012-03-05  
网上随便一查就是KFC的例子,很讨厌一个例子,一篇博客到处抄来抄去。
0 请登录后投票
   发表时间:2012-03-05  
关于KFC的例子是将一种产品有多中 实现方式。

这个例子是将一中产品很多参数是不强制需要的,是灵活组建的。
0 请登录后投票
   发表时间:2012-03-05  
感觉写代码久了, 很多情况在设计的时候都用上了所谓的模式, 但对模式的概念很模糊。
0 请登录后投票
   发表时间:2012-03-05  
不在Goods内部构建自己的零件,而交由外部如GoodBuilder去构建。
0 请登录后投票
   发表时间:2012-03-05  
没有人出来分析分析?
0 请登录后投票
   发表时间:2012-03-06  
构建者模式是对参数很多,然后只在构造器写必须的参数,其他参数可以每次调用方法绑定值,但调用时候后返回当前对象,可以连续通过操纵符.进行
0 请登录后投票
   发表时间:2012-03-06  
悲剧了 写道
构建者模式是对参数很多,然后只在构造器写必须的参数,其他参数可以每次调用方法绑定值,但调用时候后返回当前对象,可以连续通过操纵符.进行


那网上盛传的那个KFC 的例子又是怎么解释了???

0 请登录后投票
   发表时间:2012-03-06  
在命名上同为建造者模式, 为何表现区别这么大??
0 请登录后投票
   发表时间:2012-03-06  
amoszhou 写道
悲剧了 写道
构建者模式是对参数很多,然后只在构造器写必须的参数,其他参数可以每次调用方法绑定值,但调用时候后返回当前对象,可以连续通过操纵符.进行


那网上盛传的那个KFC 的例子又是怎么解释了???




把KFC例子晒出来吧
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics