Creation mode

Introduction
Creator mode may be used when the internal data is a class when too complex (for example, various attributes can only be done in a big lump what to write code for setting values), and also create such an object, rather than one according to create a variety of different scenarios, and the property type is basically the same, this time wanted to kind of what ah want another way to manage data about this class do, how to make it step by step in the creation of, and code readability well Do not let me dazzled ah, what I want also are well set in, this is the mode of application scenarios Builder, Builder models can be built and represent a class of separation.

 

The body of
the text before I see you continue to simulate a live scene, popular for everyone to explain what is the creator.

Scene: an upstart looking for an architectural design company design a commercial and a residential, we assume that these are two objects have the same commercial and residential property, such as material, floors, structure, design drawings and companies to demand to the construction unit, construction units as required to complete the construction company to design a commercial and a residential design company is very satisfied with these two objects to the upstart. Regardless of the actual situation, in the above scenario, the upstarts do not have to worry about this building which construction units, the workers built brick by brick, he was only concerned with the design firm needs mentioning, design companies will meet him, so this process shields the user and details of the construction process.

Therefore, we conclude that the model builders

Applicable scene
of the creation and use of complex objects in isolation.
A plurality of members can be fitted into one object, but the operation result is not identical.
The object or objects is very complex because of the different calling sequence and produce different effect.
When an object is initialized, too many parameters, or many parameters have default values.
Create a difference for small subjects, objects with a strong commonality.
 

The main role
without the user having to be concerned about the construction process and the details of objects you can create complex objects directly.
User-created many similar or identical properties of complex objects.
& Encapsulation reusability of code.
 

The main role of the builder pattern
product role (Product): It is a complex object that contains multiple components and attributes, is a model.

Abstract builder (Builder): it contains create an abstract method for each sub-component product interface or an abstract class, typically further includes a method that returns complex product, which method is generally abstract and return type product role (Product) .

Specific builder (Concrete Builder): achieve Builder Builder interface or inherit the abstract class, create a method to accomplish specific individual components of complex products.

Director (Director): It calls the builder component construction and assembly methods (Builder) is to complete the creation of complex objects, not product specific building design details.

to sum up:

Use the builder pattern enables the client does not have to know the details of the internal composition of the product. Easy to expand specific builders, and no dependencies between specific builder. But the disadvantage is obvious, all of the specific builder must use the same product attributes, to share a model, want to break this situation you can use the factory method or abstract factory, we have not found the factory pattern and builders do some commonalities .

Video link: https://www.bilibili.com/video/BV1ja4y1t7mU/

Guess you like

Origin www.cnblogs.com/llfnpbd/p/12587717.html