GOF defines the construction mode as separating the construction of a complex object from its representation, so that different representations can be created during the same construction process. This sentence is very abstract and hard to understand. In fact, it means to decouple the process of constructing a complex object from the components of the object. Just like a computer, accessories of any brand can be installed as long as they are compatible. Likewise, there are many ways to assemble the same accessories. This is an implementation of the spirit of reducing coupling and improving reusability.
When the product to be generated has a complex internal structure, such as consisting of multiple objects, and the system's requirements for this product may change the composition of the internal structure of the product object in the future, for example, some properties of a product are now composed of a small object, and the modified model may need N small objects. Moreover, the internal structure of the product cannot be completely exposed to the customer program, one is for availability, and the other is for security and other factors. To meet the above design environment, you can consider using the construction mode to build the framework.
Let's take a look at the composition of the construction model.
Abstract builder role: this role is used to standardize the construction of each component of a product object. Generally, this role is independent from the application's business logic. Specific builder role: this role is played by closely related classes of applications. They create product instances under the call of the mentor. This role completes product assembly and provides finished functions while implementing the methods provided by the abstract builder role. Instructor role: Call a specific builder role to create a product object. The mentor does not have the specific knowledge of the product class. What really possesses the specific knowledge of the product class is the specific builder object. Product role: complex objects under construction. It includes the classes that define components, including the interfaces that assemble these components into products.
Let's take a look at the class diagrams composed of these roles:
Expires + expires + 6687/Nu6ezzNDytNO9qNTs1d + expires + sa3oaM8L3A + expires/expires + expires/ydLUsbvM4cihtb3WuLW81d + expires = "brush: java; "> public class Media extends ArrayList {} public class Book extends Media {} public class Magazine extends Media {}
Then it contains different media composition elements:
public class MediaItem {private String name;public MediaItem(String name) {this.name = name;}public String toString(){return name;}}public class Chapter extends MediaItem {public Chapter(String name) {super(name);}}public class Article extends MediaItem {public Article(String name) {super(name);}}
The abstract builder role regulates the steps for building all media:
public abstract class MediaBuilder {public abstract void buildBase();public abstract void addMediaItem(MediaItem m);public abstract Media getFinishedMedia();}
Specific builder, building books
public class BookBuilder extends MediaBuilder {private Book book;public void buildBase() {System.out.println("Building book framework");book = new Book();}public void addMediaItem(MediaItem chapter) {System.out.println("Adding chapter " + chapter);book.add(chapter);}public Media getFinishedMedia() {return book;}}
Specific builder, construction magazine
public class MagazineBuilder extends MediaBuilder {private Magazine magazine;public void buildBase() {System.out.println("Building magazine framework");magazine = new Magazine();}public void addMediaItem(MediaItem article) {System.out.println("Adding chapter " + article);magazine.add(article);}public Media getFinishedMedia() {return magazine;}}
Instructor role, also called context
Public class MediaDirector {private MediaBuilder mb; public MediaDirector (MediaBuilder mb) {this. mb = mb; // public Media produceMedia (List input) {mb. buildBase (); for (Iterator it = input. iterator (); it. hasNext ();) mb. addMediaItem (MediaItem) it. next (); return mb. getFinishedMedia ();}}
Build different media
public class Client {public static void main(String[] args) {List input = Arrays.asList(new MediaItem[] {new MediaItem("item1"), new MediaItem("item2"),new MediaItem("item3"), new MediaItem("item4"),});MediaDirector buildBook = new MediaDirector(new BookBuilder());Media book = buildBook.produceMedia(input);String result = "book: " + book;System.out.println(result);MediaDirector buildMagazine = new MediaDirector(new MagazineBuilder());Media magazine = buildMagazine.produceMedia(input);String result2 = "magazine: " + magazine;System.out.println(result2);}}
Print result:
Building book framework
Adding chapter item1
Adding chapter item2
Adding chapter item3
Adding chapter item4
Book: [item1, item2, item3, item4]
Building magazine framework
Adding chapter item1
Adding chapter item2
Adding chapter item3
Adding chapter item4
Magazine: [item1, item2, item3, item4]
From the workflow of the construction model, the construction model "externalizes" the product assembly into the builder role. This is different from any formal factory model-product creation is completed in the product category.
The construction mode allows the appearance of products to change independently. In the original factory method model, the internal appearance of the product is determined by the product itself; in the construction model, the product is "Externalized" by the builder. In this way, defining a new specific builder role can change the internal appearance of the product and comply with the "Open and Close principle ".
The construction mode makes it unnecessary for customers to know too much internal details of the product. It encapsulates the formation and representation of complex objects in a specific construction role, and the mentor coordinates the builder role to obtain a specific product instance. Each specific builder role is irrelevant.
The construction mode allows you to precisely control the creation of complex products. The product composition is completed step by calling the specific builder role of the mentor role. Therefore, compared with other creation modes, the product construction process is better reflected.
In the construction mode, various component classes that constitute the finished product are likely to be used. For the creation of these classes, you can consider using the factory method or prototype mode for implementation, when necessary, you can add the singleton mode to control the generation of class instances. However, you must stick to the premise that the introduced model will bring benefits to your system, rather than a bloated structure.
The construction model may reference multiple different components when obtaining complex products. In this regard, the construction model is similar to the abstract factory model. The two can be distinguished from the following two points: the Creation Mode focuses on gradually assembling components into a finished product and providing finished products externally, the abstract factory model focuses on obtaining multiple product objects in the product family. The application of the abstract factory model is restricted by the product family (for details, see the simple factory model). the construction mode does not. Because the construction mode is similar to the abstract factory mode in implementing functions, the environments used by the two are complex and require more flexibility.
The Composite in the combination mode is usually composed of multiple Leaf component roles (Leaf). Therefore, the generation of the Leaf component role can be assumed by the construction mode.