标签:产品 read 构造 算法 对象 height line 实现 技术分享
把复杂对象的创建过程加以抽象,通过子类继承和重写方法。来得到具有复合属性的对象。
结构图(Struct):
生活中的样例:
import java.util.ArrayList;
抽象的创建者类。主要给详细的创建者类创建模板。详细的创建者继承该类后,重写方法。用自己的方式创建风格不同的复杂对象的属性。但整体复杂对象的结构已经固定。
abstract class Builder{ public abstract void BuildDoor(); public abstract void BuildWall(); public abstract void BuildWindows(); public abstract void BuildHouseCeiling(); public abstract House GetHouse(); }
class House{ private ArrayList <String>parts=new ArrayList<String>(); public void add(String str){ parts.add(str); } public void show() { for(String part:parts) { System.out.println(part+"\t"); } System.out.println("\n"); } }
class Director{ public void construct(Builder builder){ builder.BuildDoor(); builder.BuildWall(); builder.BuildWindows(); builder.BuildHouseCeiling(); } }
class ChineseBuilder extends Builder{ private House chineseHouse=new House(); public void BuildDoor() { chineseHouse.add("ChineseDoor"); } public void BuildWall() { chineseHouse.add("ChineseWall"); } public void BuildWindows() { chineseHouse.add("ChineseWindows"); } public void BuildHouseCeiling() { chineseHouse.add("ChineseHouserCeiling"); } public House GetHouse() { return chineseHouse; } } class EnglandBuilder extends Builder{ private House englandHouse=new House(); public void BuildDoor() { englandHouse.add("EnglandDoor"); } public void BuildWall() { englandHouse.add("EnglandWall"); } public void BuildWindows() { englandHouse.add("EnglandWindows"); } public void BuildHouseCeiling() { englandHouse.add("EnglandHouserCeiling"); } public House GetHouse() { return englandHouse; } } public class BuilderPattern { public static void main(String[] args) { Builder chineseBuilder=new ChineseBuilder(); Builder englandBuilder=new EnglandBuilder(); Director director=new Director(); director.construct(chineseBuilder); House chineseHouse=chineseBuilder.GetHouse(); chineseHouse.show(); director.construct(englandBuilder); House englandHouse=englandBuilder.GetHouse(); englandHouse.show(); } }
他们负责进行详细部件如窗户。地板的建造。
最后从民工手上收房。
本文借鉴:http://blog.csdn.net/jiafu1115/article/details/6668563
http://luchar.iteye.com/blog/179619
http://blog.sina.com.cn/s/blog_66f2257c0100i9yi.html
标签:产品 read 构造 算法 对象 height line 实现 技术分享
原文地址:http://www.cnblogs.com/lxjshuju/p/6849983.html