Design pattern reading note (2)

xiaoxiao2021-03-06  56

Builder generator mode

Name: Also called construction mode, generator mode

problem:

Usually the product is made of multiple parts, different products, constituent parts, and the assembly method is different. Suppose one product is composed of 2 parts (as for how to make it, as technology development, will be different), additional products, there are 3 parts, we need to create these products, how to achieve it?

solve:

In this way, we do the following designs:

Design an abstract builder Builder interface, standardize the components of the product: Define BuildPart1 () and BuildPart2 (), buildpart3 method.

The specific implementation of Concreate Builder is built under external requests.

The director Director class dends with the client, dividing the request for the product into Part1 / Part2, and then delegate these requests to the specific implementator.

Product product is an object that needs to be created, but there can be multiple. Each product is not the same, so it must be (generally) to achieve specific details by a specific implementation.

effect:

In this way, for customers, the diversity and complexity of the product are completely unrelated, and only the directive class object is required to achieve specific product creation through the director class instance. The director class is created by the Builder interface.

Photo:

for example:

The female, the human (product product) is composed of more than 70 parts of the soul, limbs, sensory, etc. (Part1 ... part70), female 娲 (builder builder) Commands the gods to achieve various parts, and finally return a product through the getResult method. -people.

If you are early, it may be enough, but if you make modern people, there is a definiteness, the ideas are not only pure (the product composition details are different), but it can still be composed of more than 70 parts, then the builder needs The specific implementation is different (when constructing soul, modern people are definitely different from the ancients), but they need to construct more than 70 parts (if a part does not need, just return empty)

转载请注明原文地址:https://www.9cbs.com/read-88764.html

New Post(0)