23 design patterns (4) Java generator pattern
23 design patterns Part 4: Java generator pattern
definition:
Separate the construction of a complex object from its representation, so that the same construction process can create different representations. Generator mode uses a director object and a specific builder object to build all parts one by one, so as to build a complete object.
Four elements:
Builder: Builder interface, which defines the operation of each part required to create a product object. Concretebuilder: a specific generator implementation, which realizes the creation of various parts, is responsible for assembling various parts of product object, and also provides a method for users to obtain the assembled product object. Director: the director, also known as the director, is mainly used to use the builder interface to build the required product objects in a unified process. Product: product, which represents a complex object built by the generator and contains multiple parts.
Example:
There are examples of KFC on the Internet to describe the generator mode, which is easy to understand. Suppose KFC launches two packages: Orleans drumstick burger package and spicy drumstick burger package. The set meal in Orleans includes: an Orleans chicken drumstick fort, a fried chicken wing and a glass of Sprite. The drumstick burger set meal includes: a spicy drumstick burger, a French fries and a coke. Each set meal is: staple food, non-staple food and beverage.
KFC waiters should provide packages according to customers' requirements. What is fixed and what is changed in this demand? Obviously, customers want the same package, and the customer's purpose is the same. There are staple food, non-staple food and drinks in the set meal, which is also fixed. As for what the staple food is, what the non-staple food is and what the beverage is, this is changing.
In the actual software development process, sometimes we are faced with the creation of "a complex object", which is usually composed of certain combinations of sub objects of various parts. Due to the change of requirements, Each part of this complex object or its sub objects often change (for example, customers of drumstick burger package don't like coke and want to change milk tea), but their structure is relatively stable (the package must be a staple food, non-staple food and beverage). When encountering this scenario, it is more appropriate to use the generator mode.
Define a product class:
Each small module in the product category is different. They build the product. Define n generator classes according to specific scenario requirements:
Define a commander class to uniformly dispatch the project:
advantage:
1. Using the generator pattern can make the client do not have to know the details of the internal composition of the product. 2. The specific builder classes are independent of each other, which is very beneficial to the expansion of the system. 3. Since the specific builders are independent, the construction process can be gradually refined without any impact on other modules.
Disadvantages:
The "processing process" of the builder model is exposed, which makes the builder model more flexible and makes the process opaque to customers. (to be verified, the author doesn't quite understand it here. Welcome to say your own opinion)
Application scenario:
1. Need to generate a product object with complex internal structure. Each internal component itself can be an object or a component of an object. 2. The attributes of the product object to be generated are interdependent. The construction mode can enforce a step-by-step construction process. 3. Other objects in the system will be used in the process of object creation, which are not easy to obtain in the process of product object creation
Transferred from: Java confidant
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.