Vernacular builder mode (Builder Pattern)

Foreword

Initially intended to write mode in accordance with the builder before Nissan series. But a lot of reference articles online, make me more confusion to the builder pattern, but also fear that they have been unable to understand way be interpreted. Finally, Google found an article in English Builder , make me enlightened. I had to translate this article, we hope to help understand the builder pattern.

intention

Builder mode is to create design patterns, to gradually create complex objects. Using the builder pattern may use the same code that is configured to generate different types, different representations of objects.

problem

Imagine a complex object, it requires a lot of fields and nested objects initialized. This initialization code is usually hidden in the massive constructor contains a large number of parameters.
Every possible object is to create a subclass, make the program too complex.

For example, let's build a house object. Build a simple house needs four walls, one floor, a door, a pair of windows and a roof. But if we want a bigger, brighter, and have backyards and other goodies (such as heating systems, pipes and wires) house, then how to do it?

The simplest solution is to create a class and inherit the basic House to cover all combinations of a set of parameters subclasses, but after get a lot of subclasses. Any new parameters need to further expand the hierarchy.

Another way to do subclass. You can create a constructor for all parameters. This method does not require a large number of sub-classes, there are additional problems.
大量参数的构造函数也存在问题,并非所有参数都是被需要的
In most cases, most of the parameters are not used. The code will look very ugly when this constructor is called.

solve

Construction mode is recommended that you extract the object constructor code from your class, move it to a separate object generator is called in.
建造者模式允许您逐步构造复杂的对象。构建器在构建产品时不允许其他对象访问该产品。

Builders constructed model will target organized as a set of steps (construction of the wall, built doors). A series of steps on the Builder object can create an object. The most important thing is that you do not need to call all the steps, steps need to need to call.

When you need to build a variety of product forms, some steps may require different implementations. For example, the walls can lodge built of wood, but need to build the castle walls with stones.
In this scenario, the builder can create different classes of building steps to achieve the same, different types. Then you can use these objects to the construction of different types.

不同的建造者以不同的方式执行相同的任务,生成不同的

For example, the first builder to build all wood and glass, the second with stones and iron all built, the construction of a third of all gold and diamonds. By calling the same steps that you can get from a builder where an ordinary house, get a small castle from second builder there, get a palace from the third builder there.

Director

We can further series of calls to builders steps to extract a class, this class is called Director.
Director class only defines the order of execution of steps to build, and provides a builder to achieve these steps.
Director知道执行哪些构建步骤来获得产品
Direct class is not absolutely necessary, we can directly call the Builder in a specific order. However, Director is a good way to place various construction schemes can be reused.
In addition, Director category completely hides the details of the product structure. The client only need a Builder and a Director associates can get build results.

structure

  1. Builder interface declares all types of generators are a common product configuration steps.
  2. Concrete Builders provides
    different implementations building steps.
  3. Products are objects need to generate. Different constructor constructs may belong to different product category hierarchy (inherited) or interface.
  4. Director class defines the sequence of construction steps calling, so you can create and reuse of product-specific configuration mode.
  5. Client a Builder object must associate with the Director. Typically, performed only once by the director of the arguments to the constructor. Then, director for all the objects Builder configuration. Another way is to pass Builder object to the method of the Director, may use different generators.

Scenes

  • The builder pattern for constructor Come on too long.
  • Create different representations of certain products, such as stone houses and wooden houses.
  • Complex object structure, configured to separate code and business code.

    Code

Code I did not stick around, direct access to the references in the bottom of the can.

references

https://refactoring.guru/design-patterns/builder

Guess you like

Origin www.cnblogs.com/zcr3108346262/p/12206492.html