Foreword: Looking at 9CBS gradually disappointing, it is going to write. But the teachers teach me to the end, huh, huh, then stick to it. Concept: Builder: Separate a complex object with its representation, making the same build process can create different representations. -------------------------------------------------- ------------------------------------- "Hey, Andy, come to help, help me子. ", Helen is there. "Okay, ok, 喔 ..., new machine!", Walk over, Andytao eyes are bright. Hey, seeing the new configuration of people, he has a kind of impulse. To be honest, I have a computer, and there are not a few hills who are not in the mountain. . . "Such a big girl, still don't load the machine now. How do you learn?" Andytao said so, thinking, "It is best to learn forever!" "Ok, ok, thank you!" "" " However, if you can put your mind, it is good. " Design mode? "" Listen! "" Oh ... ", Helen's mouth is really TMD cute, Andytao does not think it is not worthless. "I will tell you the Builder mode in the design mode. Thebuilder mode is a complex object step by step, which allows the user to build them only by specifying the type and content of the complex object. Users don't know the internal Specific construction details. "" Hey, this and abstract factory model is not very similar? "" Builder mode is very similar abstract factory model, the subtle difference probably only in repeated use can experience. I will talk about Builder and Abstract Some differences between Factory mode is good. "" It is generally used in both cases: 1. When creating a complex object, the algorithm of complex objects should be independent of the component of the object and their assembly mode. 2, when constructor The objects that must be allowed have different representations. The above statement is too abstract. Simply point, it is to decouple the process of building complex objects and its components to achieve a complex object constructed with it with its Indicates separation, making the same construction process can create different representations. "" Don't understand ... Specifically? "" Because a complex object, there is a large number of components, sometimes it is dizziness, take PC Machine: PC has many parts: motherboard, CPU, hard disk, display ... and various small parts, accessories, etc.; there are many parts, how to assemble these components into a PC? This assembly process is also very complicated. Need very well assembly technology (still scared you?), Then we need a corresponding method for assembly in order to organize the PC. The Builder mode we have to say is to put parts and The assembly process is generated. "" Do not understand ... "" Hey, stupid, take you no way ... "" "" "How do we talk about how to use it first!" "" "First, a complex object is made up of multiple The component consists of Builder mode is to separateize the creation of complex objects and components, respectively, and use the Builder class and the Director class.
"First, you need an interface that defines how to create complex objects: public interface builder {// Create parts A such as producing motherboard Void BuildParta (); // Creating part B, such as production cpu void buildpartb (); // Creating a component C, such as a hard disk void buildpartc (); ... // Returns the final assembly result (returning the last fitted PC) // The assembly process of the finished product is not performed here, but transferred to the Director class below. / / Thus realizing the separation of the process and components ();} Using Director to build the last complex object, and how to create one component in the Builder interface (complex object is composed of these components), That is to say, how is the content of Director to make the final assembly: public class director {private builder builder; public director (builder builder) {this.builder = builder;} // Put the parts parta partb partc finally constitute complex objects // Here It is a process of assembling the motherboard, CPU and hard disk into a PC. Build or assemble the parts; definition and clarify what specific things it wants to create; provide an interface that can re-get the product: Public Class Concretebuilder Implements Builder {Part Parta, Partb, Partc; Public Void Buildparta () {// Here Is the code of how to build parta}; public void buildpartb () {// This is a specific partb code}; public void buildpartc () {// This is a specific partb code}; public product getProduct () {// Return the final assembly result};} Complex object: Products: PUBLIC Interface Product {} Complicated object parts: public interface part {} We look at how to call Builder mode: Concretebuilder Builder = New Concretebuilder (); Director Director = New Director (Builder); Director.Construct (); Product Product = Builder.getProduct (); "" The general usage of the Builder mode is the general usage of Builder mode. As for the difference between Builder and Abstract Factory mode, Builder emphasizes the overall concept of this concept. For example, you want to get the complex object of PC, then you must first prepare the motherboard, CPU, hard disk, etc. These construction PC The necessary simple objects, then call the BUILDER's GET operation to get the final product of the PC; the Abstract Factory mode does not emphasize this relationship, or can be said to be constructed 'loosely coupled'.