Design Mode C # Language Description - Builder mode
* This article refers to some of the "Java and Mode", suitable for beginners for design patterns.
The construction mode is the creation mode of the object. The construction mode can be divided into the internal representation of a product with the generation process of the product, so that a construction process can generate a product object with different internal appearances.
One product often has different components as a part of the product, which is usually called the internal representation of the product. Different products can have different internal characters, which are different parts. Using construction mode can make customers do not need to know which parts of the objects generated, each product has different parts of each other, how to build it, and how to make up products. Here is an exemplary implementation.
In this exemplary system, the final product Product is only two parts, namely Part1 and part2. The corresponding construction method also has two: BuilderPart1 and BuilderPart2. It can be seen that this mode involves four roles:
Abstract Builder: Ges an abstract interface to standardize the buildings of the product objects.
Concrete Builder: The role is a class that is closely related to the application, and they create an instance of the product under the call of the application.
Director: It is the role that is deal with the client. The request to create a product is divided into construction requests for each part, and then delegate these requests to specific builders.
Product: The object is built.
Builder:
Public Interface Builder
{
Void BuildPart1 ();
Void BuildPart2 ();
Product retrieveresult ();
} // end interface definition builder
Concretebuilder:
Public Class Concretebuilder: Builder
{
Private products;
Public void buildpart1 ()
{
// build the first part of the product
}
Public void buildpart2 ()
{
// build the second part of the product
}
Public Product RetrieveResult ()
{
Return Product;
}
} // end class definition concretebuilder
Director:
Public Class Director
{
Private Builder Builder;
Public void Director ()
{
}
Public void construct () // Product construct method, responsible for calling each part construction method
{
Builder = new concretebuilder ();
Builder.builderpart1 ();
Builder.builderpart2 ();
Builder.RetrieveResult ();
}
} // End Class Definition Director
Product:
Public Interface Product
{
} // end interface definition products
There is only one product class in the above system, and there is also a specific builder class. There are multiple products and builders of builders, as shown below: