* ----------------------------------------- */public class acer{ protected list<string> parts = new arraylist<string>(); //生产cpu public void createcpu() { parts.add("cup: intel 酷睿i3 2350m"); } //生产内存 public void creatememory() { parts.add("内存: 4gb ddr3 1333mhz"); } //生产显卡 public void createdisplaycard() { parts.add("显卡: nvidia geforce gt 520m"); } //显示产品信息 public void show(){ system.out.print("产品部件信息:"); for(string part : parts){ system.out.print(part "\t"); } }}
* ----------------------------------------- */public class dell{ protected list<string> parts = new arraylist<string>(); //生产cpu public void createcpu() { parts.add("cup: intel 酷睿i7 3612qm"); } //生产内存 public void creatememory() { parts.add("内存: 8gb ddr3 1600mhz"); } //生产显卡 public void createdisplaycard() { parts.add("显卡: nvidia geforce gt 640m+intel gma hd 4000"); } //显示产品信息 public void show(){ system.out.print("产品部件信息:"); for(string part : parts){ system.out.print(part "\t"); } }}
* ----------------------------------------- */public class client { private static acer acer = new acer(); private static dell dell = new dell(); public static void main(string[] args){ /** * 宏基 */ acer.createcpu(); acer.creatememory(); acer.createdisplaycard(); acer.show(); /***************************************/ system.out.println(); /***************************************/ /** * 戴尔 */ dell.createcpu(); dell.creatememory(); dell.createdisplaycard(); dell.show(); } }
* ----------------------------------------- */public abstract class product { protected list<string> parts = new arraylist<string>(); //添加部件 public void add(string part){ parts.add(part); } //显示产品信息 public void show(){ system.out.print("产品部件信息:"); for(string part : parts){ system.out.print(part "\t"); } }}
* ----------------------------------------- */public class acer extends product{ //do other things here }
* ----------------------------------------- */public class dell extends product{ //do other things here }
* ----------------------------------------- */public interface builder { //cup public void buildcpu(); //内存 public void buildmemory(); //显卡 public void builddisplaycard (); //最终产品 public product getfinalresult(); }
* ----------------------------------------- */public class acerbuilder implements builder { private product product = new acer(); @override public void buildcpu() { product.add("cup: intel 酷睿i3 2350m"); } @override public void buildmemory() { product.add("内存: 4gb ddr3 1333mhz"); } @override public void builddisplaycard() { product.add("显卡: nvidia geforce gt 520m"); } @override public product getfinalresult() { return product; }}
* ----------------------------------------- */public class dellbuilder implements builder { private product product = new dell(); @override public void buildcpu() { product.add("cup: intel 酷睿i7 3612qm"); } @override public void buildmemory() { product.add("内存: 8gb ddr3 1600mhz"); } @override public void builddisplaycard() { product.add("显卡: nvidia geforce gt 640m+intel gma hd 4000"); } @override public product getfinalresult() { return product; }}
* ----------------------------------------- */public class director { private builder builder; public director(builder builder){ this.builder = builder; } public void construct(){ builder.buildcpu(); builder.buildmemory(); builder.builddisplaycard(); }}
* ----------------------------------------- */public class client { private static builder acerbuilder = new acerbuilder(), dellbuilder = new dellbuilder(); public static void main(string[] args){ system.out.print("宏基"); director director = new director(acerbuilder); director.construct(); product product = acerbuilder.getfinalresult(); product.show(); /***************************************/ system.out.println(); /***************************************/ system.out.print("戴尔"); director = new director(dellbuilder); director.construct(); product = dellbuilder.getfinalresult(); product.show(); } }