Software design patterns learning (seven) the builder pattern

Builder mode

Builder mode is the most complex to create a schema, the process of creating it clients with complex object that contains multiple components separated, clients do not need to know the internal components of the assembly part of complex objects, only need to know the type of builder It can be.


Mode motivation

The builder pattern is used to create a complex object that contains a part of, we can return to a complete product subject to the user. Users do not need to know the process of creating and internal details, simply direct the complete object can be used to create good. Has various components such as automobile wheels, steering, transmission, etc., users hardly use a single member, but the use of a complete car. Software development there are also complex objects like cars, they have a series of member properties, and there may be some restrictions.


Schema Definition

It represents the construct and its separation of a complex object, such that the same build process can create different representations. The builder pattern is a step by step to create complex objects, which allows users to build complex object by specifying the type and content, users do not need to know the internal construction of the specific details.


Mode structure

Here Insert Picture Description

  1. Builder (abstract builder)

    Product designated for each member create abstract interface object, the method buildPartX () for the respective components to create complex objects; the getResult Another method () Returns the complex objects.

  2. ConcreteBuilder (specific builder)

    Specific builders achieve Builder interface, structure and method of assembly of the components, and a clear definition of complex objects it creates, it can also provide a method to return the object to create a good product complexity.

  3. Product (Product role)

    Product is a complex character object is constructed, comprising a plurality of component parts, particularly the construction of the interior of the product creates represent and define its assembly process.

  4. Director (director)

    Order arrangement responsible for the construction of complex objects, there is any relationship between the conductor and the abstract builder can () member structure and assembling method of an object builder construction method thereof Construct call, to complete the construction of a complex object. Clients need to interact with the conductor, to determine the type of builder in the client, and specific examples of builder object (configuration file and by reflection), then set by the constructor method of the commander or class Object incoming commander class.


KFC builder mode examples of packages

  1. Example shows

    Package is a complex object, generally comprises staple (such as hamburgers, chicken roll) and beverages (juice, cola) and other components. Different packages have different components, KFC staff, according to customer needs, step by step, loading these components, construct a complete package, and then returned to the customer.

  2. Code examples and explanations

    1. Product category Meal

      Meal package is the product of complex objects, which includes two members of the attribute food and drink, which represents the staple food, drink beverages represent, Meal also includes methods Getter and Setter methods of member properties.

      public class Meal {
      
          //部件
          private String food;
          private String drink;
      
          public String getDrink() {
              return drink;
          }
      
          public void setDrink(String drink) {
              this.drink = drink;
          }
      
          public String getFood() {
              return food;
          }
      
          public void setFood(String food) {
              this.food = food;
          }
      }
    2. Abstract builder class MealBuilder (Package Builder category)

      MealBuilder package builder is, it is an abstract class, an abstract method of assembling declares buildFood member () and buildDrink (), the type of objects defined Meal in MealBuilder meal, there is provided a method factory getMeal () Returns the meal object.

      public abstract class MealBuilder {
      
          protected Meal meal = new Meal();
      
          public abstract void buildFood();
      
          public abstract void buildDrink();
      
          public Meal getMeal() {
      
              return meal;
          }
      }
    3. DETAILED class builder SubMealBuilderA (A package builder class)

      SubMealBuilderA specific builder class for creating packages A, which is a subclass of abstract class builder, component assembling method implements abstract builder class declaration.

      public class SubMealBuilderA extends MealBuilder {
      
          @Override
          public void buildFood() {
      
              meal.setFood("一个鸡腿堡");
          }
      
          @Override
          public void buildDrink() {
              meal.setDrink("一杯可乐");
          }
      }
    4. DETAILED class builder SubMealBuilderB (B Package Builder class)

      public class SubMealBuilderB extends MealBuilder {
      
          @Override
          public void buildFood() {
      
              meal.setFood("一个鸡肉卷");
          }
      
          @Override
          public void buildDrink() {
      
              meal.setDrink("一杯果汁");
          }
      }
    5. Commander class KFCWaiter (waiter category)

      KFCWaiter class is class conductor, corresponding to the waiter KFC KFC package production process, the client specifies a particular type of builder, calling member and a method of assembling factory method in which an object builder construct () method.

      public class KFCWaiter {
      
          private MealBuilder mealBuilder;
      
          public void setMealBuilder(MealBuilder mealBuilder) {
              this.mealBuilder = mealBuilder;
          }
      
          public Meal construct() {
              mealBuilder.buildFood();
              mealBuilder.buildDrink();
              return mealBuilder.getMeal();
          }
      }
    6. XML manipulation tools

      public class XMLUtil {
      
          public static Object getBean() throws Exception {
      
              //创建解析器工厂
              DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
              //创建解析器
              DocumentBuilder builder = builderFactory.newDocumentBuilder();
              //得到document
              Document document = builder.parse("config.xml");
              //获取包含品牌名称的文本节点
              NodeList brandNameList = document.getElementsByTagName("className");
              Node classNode = brandNameList.item(0).getFirstChild();
              String className = classNode.getNodeValue().trim();
      
              Class c = Class.forName("com.builderPattern." + className);
              Object o = c.newInstance();
              return o;
          }
      }
    7. Profiles

      <?xml version="1.0" encoding="UTF-8" ?>
      <configuration>
         <className>SubMealBuilderA</className>
      </configuration>
    8. Test category

      public class Test {
      
          public static void main(String[] args) throws Exception {
      
              //动态确定套餐种类
              MealBuilder mealBuilder = (MealBuilder) XMLUtil.getBean();
      
              //服务员是指挥者
              KFCWaiter waiter = new KFCWaiter();
      
              //服务员准备套餐
              waiter.setMealBuilder(mealBuilder);
      
              //客户获得套餐
              Meal meal = waiter.construct();
      
              System.out.println("套餐组成:");
              System.out.println(meal.getDrink());
              System.out.println(meal.getFood());
          }
      }
    9. Result analysis

      If the profile is set to the contents of the node SubMealBuilderA, the output results are as follows:
      Here Insert Picture Description
      If the profile is set to the contents of the node SubMealBuilderB, the output results are as follows:
      Here Insert Picture Description
      replacement of particular builder without modifying the source code, only to modify the configuration files. If you need to add new concrete builder, simply add a new concrete builder class inherits the abstract classes builder, and then implements the abstract method of assembling the components of which states, modify the configuration file, using the new configuration of the particular new builder package type, the system has good flexibility and scalability to meet the requirements of the principle of opening and closing.

Guess you like

Origin www.cnblogs.com/Yee-Q/p/12444474.html