设计模式10---建造者模式

  设计模式就是对相同部分进行归纳,归并,不仅仅包括对象(继承,多态)也包括业务逻辑,(流程结果等),以及数据存储(使用不同数据库)。

      使各个部分独立,解耦。使UI,业务逻辑,数据可以各自独立,更新等。

  对建造者模式而言就是通过构建流程与具体表现的分离,抽象出构建过程。对相同或类似的构建过程进行分离,从而达到复用的目的。

  

  Director 制定product产生的顺序,各个部分准备好了以后,可以由实体类concretebuilder产生product。

  

    public interface Builder { 
    void buildPartA(); 
    void buildPartB(); 
    void buildPartC(); 
  
    Product getResult(); 
  } 

   //具体建造工具
  public class ConcreteBuilder implements Builder { 
    Part partA, partB, partC; 

    public void buildPartA() {
      //这里是具体如何构建partA的代码
    }; 
    public void buildPartB() { 
      //这里是具体如何构建partB的代码
    }; 
     public void buildPartC() { 
      //这里是具体如何构建partB的代码
    }; 
     public Product getResult() { 
      //返回最后组装成品结果
    }; 
  }

   //建造者
  public class Director {
    private Builder builder; 
  
    public Director( Builder builder ) { 
      this.builder = builder; 
    } 
    public void construct() { 
      builder.buildPartA();
      builder.buildPartB();
      builder.buildPartC(); 
    } 
  } 


  public interface Product { }
  public interface Part { }
  

复制代码

    下面是调用builder的方法:
  ConcreteBuilder builder = new ConcreteBuilder();
  Director director = new Director( builder ); 
  
  director.construct(); 
  Product product = builder.getResult();
director.construct();是按一定的流程产生了product或者product的部件。
所以对于有相同的操作过程,但是顺序不同,或者相差几步的来说,可以通过不同的  Director 来实现。如果使用工厂方法,ConcreteBuilder 对于上层UI是透明的。

 使用抽象工厂模式封装后的代码:

public interface IBuilder {
    void buildA();
    void buildB();
    void buildC();
    
    Productor getResult();
}
public class ConcreateBuilder implements IBuilder {

    private Productor _mProductor = new Productor();
    @Override
    public void buildA() {
        // TODO Auto-generated method stub
        System.out.println("small A");
        _mProductor.setPartA("small A");
    }

    @Override
    public void buildB() {
        // TODO Auto-generated method stub
        System.out.println("middle B");
        _mProductor.setPartB("middle B");
    }

    @Override
    public void buildC() {
        // TODO Auto-generated method stub
        System.out.println("large C");
        _mProductor.setPartC("large C");
    }

    @Override
    public Productor getResult() {
        // TODO Auto-generated method stub
        return _mProductor;
    }

}
public class ConcreateBuilderB implements IBuilder {

    private Productor _mProductor = new Productor();
    @Override
    public void buildA() {
        // TODO Auto-generated method stub
        System.out.println("middle A");
        _mProductor.setPartA("middle A");
    }

    @Override
    public void buildB() {
        // TODO Auto-generated method stub
        System.out.println("small B");
        _mProductor.setPartB("small B");
    }

    @Override
    public void buildC() {
        // TODO Auto-generated method stub
        System.out.println("small C");
        _mProductor.setPartC("small C");
    }

    @Override
    public Productor getResult() {
        return _mProductor;
    }

}
public class Director {
    private IBuilder builder;

    public Director(IBuilder mBuilder) {
        builder = mBuilder;
    }

    public void construct() {
        builder.buildA();
        builder.buildB();
        builder.buildC();
    }
}
public class Productor {
    
    private String partA;
    private String partB;
    private String partC;
    /**
     * @param partA the partA to set
     */
    public void setPartA(String partA) {
        this.partA = partA;
    }
    /**
     * @param partB the partB to set
     */
    public void setPartB(String partB) {
        this.partB = partB;
    }
    /**
     * @param partC the partC to set
     */
    public void setPartC(String partC) {
        this.partC = partC;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return (partA+"\t"+partB+"\t"+partC);
    }
    
    
}

反射+抽象工厂模式:

public class BuildFactory {
    
    private static final String AssemblyName = "com.jayfulmath.designpattern.builder";
    private static final String realClass = "ConcreateBuilder";
    
    public static IBuilder operator()
    {
        IBuilder result = null;
        String className =AssemblyName+"."+realClass;
        try {
            Class<?> c = Class.forName(className);
            Constructor<?> ct = c.getConstructor();
            result = (IBuilder) (ct.newInstance());
        } catch (NoSuchMethodException |ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return result;
    }
}
/*对于构建过程,可以使用Director来固定
 *对于实现类ConcreateBuilder and  ConcreateBuilderB, 可以使用抽象工厂模式的方式进行封装.
 * 
 * */
public class BuildMain extends BasicExample {

    @Override
    public void startDemo() {
        // TODO Auto-generated method stub
        IBuilder builder = BuildFactory.operator();
        Director mDirector = new Director(builder);
        mDirector.construct();
        Productor mProductor = builder.getResult();
        System.out.println("productor:"+mProductor.toString());
    }

}

可以看到,main方法只要知道IBuilder以及director,由mDirector.construct();来构造,就可以获得Productor .

posted on 2014-12-01 14:33  Joyfulmath  阅读(163)  评论(0编辑  收藏  举报

导航