标签:
生活中的例子:
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
版权声明:欢迎转载,希望在你转载的同时,添加原文地址,谢谢配合
标签:
原文地址:http://blog.csdn.net/u011225629/article/details/47752323