首页 > 代码库 > 大话设计模式第十三章之建造者模式
大话设计模式第十三章之建造者模式
package com.build; /** * 建造胖子 * @author 黄二狗 * */ public class PersonFatBuilder { public PersonFatBuilder() { } public void build() { System.out.println("大头"); System.out.println("大腿"); System.out.println("粗腰"); } }
package com.build; /** * 建造瘦人 * @author 黄二狗 * */ public class PersonThinBuilder { public PersonThinBuilder() { } public void build() { System.out.println("小头"); System.out.println("小腿"); System.out.println("细腰"); } }
package com.build; public class Client { //出场 public static void main(String[] args) { //建造胖子 PersonFatBuilder fatBuild=new PersonFatBuilder(); //建造瘦子 PersonThinBuilder thinBuild=new PersonThinBuilder(); fatBuild.build(); System.out.println("========="); thinBuild.build(); } }
上面代码的缺点是流程不够规范 在一个build方法里面还是有可能出错。
package com.build; public interface PersonBuilder { public abstract void buildHead(); public abstract void buildBody(); public abstract void buildArmLeft(); public abstract void buildArmRight(); public abstract void buildLegLeft(); public abstract void buildLegRight(); }
package com.build; /** * 建造胖子 * @author 黄二狗 * */ public class PersonFatBuilder implements PersonBuilder{ public PersonFatBuilder() { } @Override public void buildHead() { System.out.println("大头"); } @Override public void buildBody() { System.out.println("大身体"); } @Override public void buildArmLeft() { System.out.println("左大胳膊"); } @Override public void buildArmRight() { System.out.println("右大胳膊"); } @Override public void buildLegLeft() { System.out.println("左大腿"); } @Override public void buildLegRight() { System.out.println("右大腿"); } }
package com.build; /** * 建造瘦人 * @author 黄二狗 * */ public class PersonThinBuilder implements PersonBuilder{ public PersonThinBuilder() { } @Override public void buildHead() { System.out.println("小头"); } @Override public void buildBody() { System.out.println("小身体"); } @Override public void buildArmLeft() { System.out.println("左小胳膊"); } @Override public void buildArmRight() { System.out.println("右小胳膊"); } @Override public void buildLegLeft() { System.out.println("左小腿"); } @Override public void buildLegRight() { System.out.println("右小腿"); } }
package com.build; /** * 建造指导者 如果没有该类 用户需要知道怎么建出来的 但是用户根本就不想知道这些 * @author 黄二狗 * */ public class PersonDirector { private PersonBuilder pb; public PersonDirector(PersonBuilder pb) { this.pb=pb; } //流程规范 public void createPerson() { pb.buildHead(); pb.buildBody(); pb.buildArmLeft(); pb.buildArmRight(); pb.buildLegLeft(); pb.buildLegRight(); } }
package com.build; public class Client { //出场 public static void main(String[] args) { PersonFatBuilder pfb=new PersonFatBuilder(); PersonDirector pdThin=new PersonDirector(pfb);
package com.build2; import java.util.ArrayList; import java.util.List; //产品 由多个部件组成 public class Product { List<String> parts=new ArrayList<String>(); public void add(String part) { parts.add(part); } public void show() { System.out.println("产品创建===="); for(String part:parts) { System.out.println(part); } } }
package com.build2; public interface Builder { //假设产品由两个部件组成 void buildPartA(); void buildPartB(); Product getResult(); }
package com.build2; public class ConcreteBuilder1 implements Builder{ private Product product=new Product(); @Override public void buildPartA() { product.add("部件A"); } @Override public void buildPartB() { product.add("部件B"); } @Override public Product getResult() { return product; } }
package com.build2; public class ConcreteBuilder2 implements Builder{ private Product product=new Product(); @Override public void buildPartA() { product.add("部件X"); } @Override public void buildPartB() { product.add("部件Y"); } @Override public Product getResult() { return product; } }
package com.build2; public class Director { public void construct(Builder builder) { builder.buildPartA(); builder.buildPartB(); } }
package com.build2; public class Client { public static void main(String[] args) { Director director=new Director(); Builder builder=new ConcreteBuilder1(); director.construct(builder); Product product=builder.getResult(); product.show(); } }
pdThin.createPerson(); System.out.println("==========="); PersonThinBuilder ptb=new PersonThinBuilder(); PersonDirector pdFat=new PersonDirector(ptb); pdFat.createPerson(); } }
package com.build2; import java.util.ArrayList; import java.util.List; //产品 由多个部件组成 public class Product { List<String> parts=new ArrayList<String>(); public void add(String part) { parts.add(part); } public void show() { System.out.println("产品创建===="); for(String part:parts) { System.out.println(part); } } }
package com.build2; public interface Builder { //假设产品由两个部件组成 void buildPartA(); void buildPartB(); Product getResult(); }
package com.build2; public class ConcreteBuilder1 implements Builder{ private Product product=new Product(); @Override public void buildPartA() { product.add("部件A"); } @Override public void buildPartB() { product.add("部件B"); } @Override public Product getResult() { return product; } }
package com.build2; public class ConcreteBuilder2 implements Builder{ private Product product=new Product(); @Override public void buildPartA() { product.add("部件X"); } @Override public void buildPartB() { product.add("部件Y"); } @Override public Product getResult() { return product; } }
package com.build2; public class Director { public void construct(Builder builder) { builder.buildPartA(); builder.buildPartB(); } }
package com.build2; public class Client { public static void main(String[] args) { Director director=new Director(); Builder builder=new ConcreteBuilder1(); director.construct(builder); Product product=builder.getResult(); product.show(); } }
大话设计模式第十三章之建造者模式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。