Frustrated, the younger brother came over and said, “What the hell is Builder mode?” Don’t understand! Don’t understand!

I smiled and knew what he was thinking. So it says: Are all the blogs you see on the Web the standard model of the Builder model: the Builder abstract class, the actual creator, the manager. Or, to simplify things a little, give the user control over the build sequence?

The younger brother nodded yes.

I continued: Has anyone ever thrown you the magic phrase “separating the construction of a complex object from its representation so that the same construction process can create different representations”?

Brother: I know all these words, but I don’t even know what one of them means. Will you or won’t you? Don’t fuck with me!

I snort: To explain the Builder pattern, you have to talk about its application scenarios. Come on, give me an example!

Start loading 13: Stir-fry there are some scenes, respectively

Action 1: add water; Action two: Refueling; Action three: add salt; Action 4: Add sugar. Finally, label the dish to let people know what it is, avoid confusion, picture!!

  1. Emperor: I want to eat zhang SAN to do the first filled Buddha jump wall!
  2. The head chef got the message and made a list: “First Buddha Jumps over the Wall: Bowl with Sugar Buddha Jumps over the Wall; Label.” “And threw the list to Joe.
  3. Cook Zhang SAN according to the chef’s list, the dishes made to the chef;
  4. The chef brought the dishes to the emperor.

Brother: What’s the point of going around and around?

Me: Of course, I can’t see the advantages of eating alone, but when there are too many people, there is no chef to force control, and cooking directly (setting the attributes of the dishes) will run the risk of forgetting, for example, forgetting to add labels. Although the dishes are made, they forget what they are and cannot be served. In this case, the attribute is relatively simple, and when the object has many attributes and creates the instance directly, the assignment will be complicated, and it is easy to miss some attributes. There is a sense of group, each flavor of the dish, to set the different attributes together. Easy to control the details of the risk.

Second: we can also encapsulate multiple set methods into one process of the chef, so that the user does not need to know the details of the inside of the dish, just know that the process is being performed.

Finally: The cook is self-contained and easily extensible, as long as the cook knows the recipe (implement abstract classes)

Brother: So easy! I’m going to create an instance with some attributes, so I’m going to use factory mode. Creator mode not to write! My stupid brother, I thought to myself. They say, “There are similarities between the two models, but the factory model focuses on cooking the dish, as long as it is the dish.” Built model focuses on the details of the carve, as Buddha jumps over the wall, add a increase artistic carving furnishing articles, sprinkle dry ice ethereal feeling, create smoke plate write calligraphy enhance the connotation of culture and so on To summarize: factory pattern emphatically in the same series of instances of the creation of a whole, while the fabricant model notice for details when creating an object instance of the control.

Brother: I see! We need steamed lamb, steamed bear paw, steamed deer tail, roasted duck… For example, factories need specific acids to need specific sour steamed lamb, sweet steamed lamb, bitter steamed lamb, spicy steamed lamb.. Builder mode!

I: bicker strong, terror so!!

Lot code