In-depth development mode

xiaoxiao2021-03-06  40

First, the primer

A few days ago, accompanied by friends to put a computer, looked at the machinery installed the machine, can't help but think of the construction mode. As a loader, they don't have to take the CPU you use is Intel or AMD, no matter whether your graphics card is 2,000 thousand yuan or white, you can be equipped with two five uniforms - a PC is born ! Of course, for customers, you don't know too much about the details of PC assembly. What is this similar to the construction mode!

Today, come to the construction mode.

Second, definition and structure

GOF is defined as: separating a complex object construct with its representation, making the same build process can create different representations. The essence of the construction mode can be summarized as: decoupling components of the process and objects of the complex object. This is an implementation of reducing coupling and improving the spirit of reusability. In fact, this spirit is implemented in almost all design patterns of GOF.

Is it the same as the installed process mentioned above?

Many people think that the construction mode similar to the abstract factory model is used in what kind of design environment (for the two more discussions)? I think it can be summarized as the following environment: When the product to be generated has a complex internal structure, the internal structure consists of multiple objects; the system may change the composition or implementation of the internal structure of the product object, such as some product The attribute is now obtained from the database, and the future may be resolved from XML; and the internal construction of the product is not fully exposed to the client program, one is for availability, and the other is for security. Meet the above design environment can consider using construction mode to build a frame.

Let's take a look at the composition of the construction mode.

Abstract constructor role: This role is used to standardize the construction of the various composition components of the product object. In general, this role is independent of the commercial logic of the application.

Specific builder role: A class that is closely related to this role is a class that is closely related to the instructor's call. This role reaches the completion of the product assembly, providing finished product assembly with the method of implementing the abstract builder role.

Instructor role: Call the specific builder role to create a product object. The instructor does not have the specific knowledge of the product class, and the specific knowledge of the product class is the specific builder object.

Product role: complex object in the construction. It wants to include classes that define components, including the interfaces of these components into the product.

Look at the class diagrams of these characters:

First, the customer program creates a instructor object, a built-in role, and configuring the builder role into the instructor object. The instructor then creates a product according to the steps to call the builder's method. Finally, the client gets the product from the builder or the instructor.

From the construction mode workflow, construction mode will "externalize" to the builder role. This is not the same as any formal factory model - product creation is done in the product class.

Third, realize

I can't find a good example, I think "Java and Mode" example is still possible. Here I put the example in "Think In Patterns with Java" to the right and charge the facade. Media can have different forms of expression, such as books, magazines, and networks. This example indicates that the steps of different forms of media constructs are similar, so they can be extracted into the instructor role.

Import java.util. *;

Import junit.framework. *;

// Different media forms:

Class Media Extends ArrayList {}

Class Book Extends Media {}

Class Magazine Extends Media {}

Class Website Extends Media {}

// Further, there is no different media constituent elements:

Class MediaItem {

PRIVATE STRING S;

Public MediaItem (String S) {this.s = s;}

Public String toString () {Return S;}}

Class Chapter Extends MediaITEM {

Public Chapter (String S) {Super (s);

}

Class Article Extends MediaItem {

Public Article (String S) {Super (s);

}

Class WebItem Extends MediaITEM {

Public WebItem (String s) {Super (s);

}

// Abstract builder role, which specifies the steps of all media construction:

Class MediaBuilder {

Public void buildbase () {}

Public void addmediaitem (MediaItem item) {}

Public Media getFinishedMedia () {return null;}

}

// Specific builder role

Class Bookbuilder Extends MediaBuilder {

Private book b;

Public void buildbase () {

System.out.println ("Building Book Framework");

B = New book ();

}

Public void addmediaItem (MediaItem chapter) {

System.out.Println ("Add Chapter" Chapter;

B.Add (chapter);

}

Public Media getFinishedMedia () {Return B;

}

Class MagazineBuilder Extends MediaBuilder {

PRIVATE MAGAZINE M;

Public void buildbase () {

System.out.Println ("Building Magazine Framework);

M = new magazine ();

}

Public void addmediaitem (MediaItem Article) {

System.out.println ("Adding Article" Article;

m.add (article);

}

Public Media getFinishedMedia () {Return M;

}

Class WebsiteBuilder Extends MediaBuilder {

Private Website W;

Public void buildbase () {

System.out.Println ("Building Web Site Framework");

w = new Website ();

}

Public void addmediaITEM (MediaITEM WebItem) {

System.out.println ("Adding Web Item" WebItem;

W.ADD (WebItem);

}

Public Media getFinishedMedia () {Return W;

}

/ / The instructor character is also called context

Class MediaDirector {

Private MediaBuilder MB;

Public MediaDirector (MediaBuilder MB) {

THIS.MB = MB; / / has a strategy mode similar characteristic

}

Public Media ProduceMedia (List Input) {

Mb.buildbase ();

ITERATOR IT = INPUT.ITERATOR (); it.hasnext ();) Mb.addmediaItem ((MediaItem) it.next ());

Return mb.getfinished;

}

}

// Test Program - Customer Role

Public class buildmedia extends Testcase {

Private list input = arrays.aslist (new mediaItem "{

New MediaItem ("item1"), New MediaItem ("Item2"),

New MediaItem ("item3"), New MediaItem ("item4"),

});

Public void testbook () {

MediaDirector buildbook =

New MediaDirector (New Bookbuilder ());

Media book = buildbook.producemedia (input);

String result = "book:" book;

System.out.println (Result);

Assertequals (Result,

"Book: [Item1, Item2, Item3, Item4]");

}

Public void testmagazine () {

MediaDirector buildmagazine =

New MediaDirector (New MagazineBuilder ());

Media magazine = buildmagazine.producemedia (input);

String result = "Magazine:" Magazine;

System.out.println (Result);

Assertequals (Result,

"Magazine: [Item1, item2, item3, item4]");

}

Public void testwebsite () {

MediaDirector buildwebsite =

New MediaDirector (New WebsiteBuilder ());

Media Website = BuildWebsite.ProduceMedia (Input);

String result = "Web Site:" Website;

System.out.println (Result);

Assertequals (Result,

"Web Site: [Item1, Item2, Item3, Item4]");

}

Public static void main (String [] args) {

JUnit.textui.teStrunner.run (buildmedia.class);

}

}

At the time of implementation, the interface provided by the abstract construction role must be adapted to accommodate different specific construction roles. For a construction role, some steps may be unwanted, which can be implemented empty. There may be no very many common points between multiple products, and a marking interface can be provided as an abstract product role; it is also possible to provide an abstract product role. At this time, the interface to provide the product is removed from the abstraction role, otherwise it will compile Problem.

Fourth, application advantages

The construction mode can make the item inside the product varies independently. In the original factory method mode, the item inside the product is determined by the product itself; and "externalization" is "externalization" is responsible for the builder. This defines a new concrete builder role to change the internal representation of the product, in line with the "opening and closing principle". The construction model makes the customer do not need to know the details inside the product. It encapsulates the composition and representation of complex objects in a specific construction role, and by the instructor to coordinate the builder role to get specific product instances.

Each specific builder role is unrelated.

Construction mode can be more refined to create complex products. The composition of the product is gradually completed by the instructor role to call the specific builder role, so it is better to reflect the construction process of the product compared to other creation modes.

Five, expand

It is likely to use various component classes that make up the finished product, and the creation of these classes can be used to implement the use of factory methods or prototype modes. It can also be added to control class instance when necessary. But to stick to a big premise is to bring the introduced model to your system, not a bloated structure.

Construction mode may be referenced by multiple different components when getting complex products, in which this, construction mode and abstract factory model are similar. It can be divided from the following two points: Create mode focuses on gradually assembling components into a finished product and provides finished products outward, and abstract plant model focuses on multiple product objects related to product families; abstract factory models Restricted to the product family (specifically see "in-depth plants"), the construction mode will not.

Since the construction mode and abstract factory model are realized, the environment used is complicated and more flexible.

In the construction mode, you may need to use a component class with different "Size", so it is often used in the synthesis mode.

Six, summary

Write some awareness of construction model, I hope to give you a more comprehensive introduction. If there is any deficiencies and 纰, please don't finish correct!

转载请注明原文地址:https://www.9cbs.com/read-66916.html

New Post(0)