23 design modes (4)-generator mode and 23 design modes
Definition:
Separates the construction of a complex object from its representation, so that different representations can be created during the same construction process. The generator mode creates all parts one by one using a director object and a specific builder object to create a complete object.
Four elements:
Builder: a Builder interface that defines the operations on each part required to create a Product object.
ConcreteBuilder: a specific builder is used to create parts and assemble the parts of the Product object. It also provides a method for users to obtain the Product objects after assembly.
Director: the mentor, also known as the guiding person, is mainly used to use the Builder interface to build the required Product objects in a unified process.
Product: a Product that represents a complex object built by a generator and contains multiple components.
Example:
KFC examples can be used on the Internet to describe the generator mode, which is easy to understand.
Assume that KFC has two types of packages: Chicken leg Fort package and spicy chicken leg Fort package.
The starter package includes a chicken leg burger, chicken wings, and a Sprite.
The chicken leg burger package includes: a spicy chicken leg burger, a fries, and a cup of cola.
Each package includes staple food, complementary food, and drinks.
The KFC attendant needs to provide the package according to the customer's requirements. What is fixed in this requirement and what is changed? Obviously, customers all require a package with the same purpose. The package contains staple food, complementary food, and drinks, which is also fixed. As for what the staple food is, what the secondary food is, and what the drinks are, this is a change.
In the actual process of software development, sometimes it is faced with the creation of a "complex object", which is usually composed of the sub-objects of each part using a certain combination. due to changes in requirements, each part of the complex object or its sub-objects must change frequently (for example, customers of the chicken leg Fort package do not like cola and want to change milk tea ), but their structure is relatively stable (the package has to be a staple food, complementary food and drinks ). In this scenario, it is appropriate to use the generator mode.
Define a product category:
public class Entity1{...}
public class Entity2{...}
public class Entity3{...}
public class Product{ Entity1 entity1; Entity2 entity2; Entity3 entity3;}
Each small module in the product category is different. They build the product.
Define n generator classes according to specific scenario requirements:
public interface IBuild{
public void createEntity1();
public void createEntity2();
public void createEntity3();
public Product composite();
public Product create(); }
public class BuildProduct implements IBuild{ Product p = new Product();
public void createEntity1(){
//p.entity1 = ... }
public Product create(){
return composite(); } ......}
public class BuildProduct1 implements IBuild{ Product p = new Product();
public void createEntity1(){
//p.entity1 = ... } ......}
Define a conductor class for unified project scheduling:
public class Director{
private IBuild build;
public Director(IBuild build){
this.build = buid; }
public Product build(){ build.create(); }
public static void main(){ IBuild build = new BuildProduct(); Director direcotr = new Director(build); Prodcut p = director.build(); }}
Advantages:
1. Using the generator mode, the client does not have to know the details of the product composition.
2. The specific builder classes are independent of each other, which is very beneficial to system expansion.
3. Because the specific builder is independent, the construction process can be gradually refined without any impact on other modules.
Disadvantages:
The "processing process" of the builder mode is exposed, which makes the builder mode more flexible and the process becomes non-transparent to the customer. (I am not very familiar with it for further research. You are welcome to express your opinions)
Application scenarios:
1. A product object must be generated with a complex internal structure. Each internal component can be an object or an integral part of an object.
2. The properties of the product objects to be generated are mutually dependent. The construction model enforces a step-by-step construction process.
3. Other objects in the system will be used during object creation. These objects are not easy to obtain during product object creation.