首页 > 代码库 > 大话设计模式第十三章之建造者模式

大话设计模式第十三章之建造者模式

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();
     
}
}

技术分享

 

大话设计模式第十三章之建造者模式