Dot Net Design Mode-generator Mode

Source: Internet
Author: User
Tags dot net
1. Summary
1.1 intent
Separates the construction and representation of complex objects so that different representations can be created during the same construction process. Note the following points.
(1) Construction and representation separation: indicates the structure of the generator mode. The construction process is encapsulated in the navigator, And the generator is responsible for implementing the specific representation.
(2) The same build process: the builder mode focuses on the build process, that is, the build process is the same.
(3) different representations: The generator mode does not care about the results of the generated objects. The products constructed by the generator do not necessarily have the same type.

1.2 usage
The algorithm for creating a complex object should be independent of the components of the object and its assembly method, and the construction process must allow the builder mode to be used when the constructed object has different representations.

1.3 Structure
The structure of the generator mode.

(1) Builder: specify an abstract interface for each part of the Product object.
(2) ConcreteBuilder: implements the Builder interface to construct and assemble each part of the product, define and
Define the representation it creates and provide an interface for searching products.
(3) Director: constructs an object using the Builder interface.
(4) Product: a complex object to be constructed. ConcreteBuilder creates an internal representation of the product and sets
The assembly process includes the class that defines the components and the interfaces that assemble these components into the final product.

1.4 Implementation example
Use a simple example to illustrate the Code Implementation of the generator mode. Write a Facebook painting program to break down the face painting process into face painting, eyes painting, nose painting, ears painting and mouth painting. First, create an abstract generator named FaceBuilder: using System;
Namespace FaceBuildernamespace FaceBuilder
{
Public abstract Class FaceBuilderclass FaceBuilder ()
{}
Public abstract void BuildFace ();
Public abstract void BuildEyes ();
Public abstract void BuildNose ();
Public abstract void BuildEars ();
Public abstract void BuildMouth ();
}

Then create a specific generator and use GUI + to draw the Facebook: using System;
Using System. Drawing;
Namespace FaceBuildernamespace FaceBuilder
{
Public Class GUIFaceBuilderclass GUIFaceBuilder: FaceBuilder
{
Private Graphics grap;
Private Pen p;
Public GUIFaceBuilder (Graphics g)
{
Grap = p;
P = new Pen (Color. Blue );
}
Public override void BuildFace ()
{
Grap. DrawEllipse (p, 100,100,200,300 );
}
Public override void BuildEyes ()
{
Grap. DrawEllipse (p, 125,220, 50, 30 );
Grap. DrawEllipse (p, 225,220, 50, 30 );
}
Public override void BuildEars ()
{
Grap. DrawEllipse (p, 80,250, 20, 40 );
Grap. DrawEllipse (p, 300,250, 20, 40 );
}
Public override void BuildNose ()
{
Grap. DrawEllipse (p, 190,250, 20, 50 );
}
Public override void BuildNose ()
{
Grap. DrawEllipse (p, 190,350 );
}
}
}

Next, create the drawing navigation: using System;
Namespace FaceBuildernamespace FaceBuilder
{
Public Class FaceDirectorclass FaceDirector
{
Private Class FaceBuilderclass FaceBuilder B;
Public FaceDirector (FaceBuilder B)
{
This. B = B;
}
Public void CreateFace ()
{
B. BuildFace ();
B. BuildEyes ();
B. BuildEars ();
B. BuildNose ();
B. BuildNose ();
}
}
}

Finally, you can call it in a form:
GUIFaceBuilder B = new GUIFaceBuilder (this. CreateGraphics ());
FaceDirector c = new FaceDirector (B );
C. CreateFace ();
Structure

2. Results
Using the generator mode can easily change the internal representation of the product, and the face spectrum in the above example is not very good. However, if we create a new generator and visually draw parts of Facebook, we can improve the quality of Facebook. This work can be completely separated from the main program, and is independently completed by an art-loving programmer.
The builder mode separates the constructor code from the representation code. A Facebook can be represented by bitmap, string, or object model, and the constructed code is exactly the same.
The construction process can be controlled more precisely. The generator mode emphasizes the product construction process, and each part of the product is dependent.
Note that the objects generated by different generators may not belong to the same type. Therefore, the user who uses the generators must know the specific type of the product. For example, in the example of creating a face filter, the generated GUI face filter and face filter are completely different types. This means that generators are often not interchangeable, and different generators target different customer programs.

3. Differences between generator mode and Abstract Factory
The generator mode focuses on separating the process of constructing an object from the parts of the constructor, while the abstract factory focuses on building a product series. In fact, the biggest difference is that products created in the generator mode do not necessarily have a common parent class, as long as there is a similar construction process. In the above example, FaceBuilder only abstracts the build process and does not specify the product type that must be generated. The specific product is determined by the specific Builder class. From a simple Graphics object generated in the above example, we can obtain a Facebook bitmap, but we can also obtain a Face object using the same navigator and structure.
In fact, we can use the generator mode to implement the common file resource manager. Due to the time relationship, we will not talk about it here. This may be involved in future models.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.