Design Mode: construction mode; Design Mode Construction
The building mode is the object creation mode. The building mode can separate the internal appearance of a product from the production process of the product. Instead, a building process generates a product with a different internal appearance.
Object.
The construction mode structure is shown in the following class diagram:
In this system, the final Product has only two parts, namely, part1 and part2. There are also two construction methods: buildPart1 () and buildPart2 (), in addition, this mode involves four roles:
* Abstract Builder role: this role provides an abstract interface to standardize the construction of each component of a product object. Generally, this interface is independent of the business logic of the application.
* Concrete Builder role: this role is a number of classes closely related to applications. They create product instances under application calls.
* Director role: the class that assumes this role calls a specific builder role to create a product object. It should be noted that the Director role does not have specific knowledge about the product class.
The specific knowledge of the product category is the specific builder role.
* Product role: a Product is a complex object under construction. Generally, there are more than one Product category in a system, and these Product categories do not necessarily have the same
But it can be completely unrelated.
The following is a simple schematic source code for this system:
Director:
Package builder; public class Director {private Builder builder;/*** product construction method, which calls the construction method of each part */public void construct () {builder = new ConcreteBuilder (); builder. buildPart1 (); builder. buildPart2 (); builder. retrieveResult (); // continue with other code }}
Builder:
Package builder; public abstract class Builder {/*** product part Construction Method */public abstract void buildPart1 (); /*** Product part Construction Method */public abstract void buildPart2 ();/*** Product return Method */public abstract Product retrieveResult ();}
ConcreteBuilder:
Package builder; public class ConcreteBuilder extends Builder {private Product product = new Product ();/*** Product part Construction Method */public void buildPart1 () {// build the first part of the part}/*** product part Construction Method */public void buildPart2 () {// build the second part of the product}/*** return Method */public Product retrieveResult () {return product ;}}
Product:
package builder;public class Product {//Anything pertaining to this product}
In these source codes, many methods are not implemented, and the Product class does not have any attributes or methods, but does not affect its readability. In the specific construction process, the corresponding product appears.
And the corresponding implementation process.
This is a simple construction model.