Design Patterns (5) - Detailed Explanation of Builder Patterns (Easy to Understand)

Builder Pattern Definition

Definition: The Builder pattern is a design pattern that separates the construction of a complex object from its representation so that the same construction process can create different representations.

First analyze the four parts on the model diagram:

  • Product: The product class, which refers to the complex object to be created, usually implements the template method pattern.

  • Builder: An abstract construction class that regulates the formation of products, generally implemented by subclasses.

  • ConcreteBuilder: A concrete construction class that implements the methods defined by the abstract construction class and returns a constructed object.

  • Director: The director class, that is, the conductor, is responsible for arranging the process.

Example description

Take building a house as an example: it is assumed here that two types of houses are built, a villa and an ordinary house; the construction process sequence of the two types is different; the villa has three steps of ABC, and the ordinary house has three steps of CBA.

  1. First define the Product class, VillaModel and OrdinaryhouseModel

(1). Abstract product class

public abstract class HouseModel  {
   //Define the order of execution, put it in the collection
   private List<String> OrderList= new ArrayList<>();

   //The steps of building a house, in no particular order, different houses, different orders
   protected  abstract  void buildstepA () ;
   protected  abstract  void buildstepB () ;
   protected  abstract  void buildstepC () ;
  /*
  build house
   */

   final   public void build () {
       for ( int i= 0 ;i<this.OrderList.size();i++){
           String name =this.OrderList.get(i);
           if (name.equalsIgnoreCase("stepA")){
               this.buildstepA();
           }else if (name.equalsIgnoreCase("stepB")){
               this.buildstepB();
           }else if (name.equalsIgnoreCase("stepC")){
               this.buildstepC();
           }
       }

   }
   //设置顺序
   public void setOrderList(List<String> orderList) {
       OrderList = orderList;
   }
}

 

(2). Two implementation classes

public class VillaModel extends HouseModel {
   @Override
   protected void buildstepA() {
    System.out.println("别墅建造步骤A操作");  
   }

   @Override
   protected void buildstepB() {
       System.out.println("别墅建造步骤B操作");
   }

   @Override
   protected void buildstepC() {
       System.out.println("别墅建造步骤C操作");
   }
}


public class OrdinaryhouseModel extends HouseModel {
   @Override
   protected void buildstepA() {
       System.out.println("普通房屋建造步骤A操作");
   }

   @Override
   protected void buildstepB() {
       System.out.println("普通房屋建造步骤B操作");
   }

   @Override
   protected void buildstepC() {
       System.out.println("普通房屋建造步骤C操作");
   }
}

 

2.定义Builder抽象建造类,也就是写两种房屋的建造方式.

public abstract class HouseBuilder  {
   //为了设置建造顺序
   public  abstract  void  setOrderList(List<String> orderList);
   
   //获取安排完建造顺序的对应的房屋
   public  abstract  HouseModel getHouseModel();
}

 

3.定义ConcreteBuilder具体建造类,也就是两种房屋建造类实现上面定义的Builder

//别墅
public class VillaBuilder extends HouseBuilder {
   private  VillaModel villaModel=new VillaModel();
   @Override
   public void setOrderList(List<String> orderList) {
       this.villaModel.setOrderList(orderList);
   }

   @Override
   public HouseModel getHouseModel() {
       return this.villaModel;
   }
}

//普通房屋
public class OrdinaryhouseBuilder extends HouseBuilder {
   private  OrdinaryhouseModel ordinaryhouseModel=new OrdinaryhouseModel();
   @Override
   public void setOrderList(List<String> orderList) {
       this.ordinaryhouseModel.setOrderList(orderList);
   }

   @Override
   public HouseModel getHouseModel() {
       return this.ordinaryhouseModel;
   }
}

 

注意:上面的准备工作都做完了,为了更深刻的理解Director<导演类>,我们先不加Director,直接去实现此实例。

不加入Director类,实现方式就是直接设置顺序,以建造别墅为例,代码如下:

       List<String> OrderList=new ArrayList<>();
       OrderList.add("stepA");
       OrderList.add("stepB");
       OrderList.add("stepC");

       VillaBuilder villaBuilder=new VillaBuilder();
       villaBuilder.setOrderList(OrderList);

       VillaModel villaModel= (VillaModel) villaBuilder.getHouseModel();
       villaModel.build();

这还只是别墅的建造,代码是不是很臃肿?所以封装是必须的,既加上Director类来指挥(封装)顺序。

 

4.定义Director类,也就是对已知的两种房屋的建造步骤进行封装

public class Director {
   private List<String> OrderList=new ArrayList<>();
   private  OrdinaryhouseBuilder ordinaryhouseBuilder=new OrdinaryhouseBuilder();
   private  VillaBuilder villaBuilder=new VillaBuilder();
/*
普通房屋建造步骤封装
*/

   public OrdinaryhouseModel getHouseModel(){
       this.OrderList.clear();
       this.OrderList.add("stepC");
       this.OrderList.add("stepB");
       this.OrderList.add("stepA");
       this.ordinaryhouseBuilder.setOrderList(OrderList);
       return (OrdinaryhouseModel) this.ordinaryhouseBuilder.getHouseModel();
   }
   /*
   别墅建造步骤封装
    */

   public VillaModel getvillaModel(){
       this.OrderList.clear();
       this.OrderList.add("stepA");
       this.OrderList.add("stepB");
       this.OrderList.add("stepC");
       this.villaBuilder.setOrderList(OrderList);
       return (VillaModel) this.villaBuilder.getHouseModel();
   }

}

 

这时实现实例的代码如下:

       Director director=new Director();
       director.getHouseModel().build();
       director.getvillaModel().build();

运行结果:

 

加入了Director类后调用是不是感觉很简单,清晰了。另外如果两种房屋的建造步骤有变化了,可以直接在Director类封装新的顺序。

建造者模式应用

1.实用范围

  • 当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时。

  • 当构造过程必须允许被构造的对象有不同表示时。

 

2.建造者模式的优缺点

(1).优点

  • 封装性好

  • 具体建造类之间独立,扩展性好

(2).缺点

产生多余的Build对象以及Dirextor类。

 

3.建造者模式与工厂模式的区别

这两种模式很相似,建造者模式更注重于方法的调用顺序,而工厂模式注重于创建产品。具体的区别还是推荐大家去亲身去了解一下这两种模式。

文章学习参考了《设计模式之禅》《Android进阶之光》

推荐阅读

 

    

    程序员如何扩展IT视野,把握最新技术资讯 

    

    设计模式(2)-工厂方法模式详解(易懂)

    

    设计模式(3)-抽象工厂模式详解(易懂)

 

    设计模式(4)-模板方法模式详解(易懂)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326022484&siteId=291194637