Design Patterns Learning Notes (12: Generator mode)

Source: Internet
Author: User

1.1 Overview

separates the construction of a complex object from its representation so that the same build process can create different representations. This is the definition of the producer pattern.

If there are several member variables in a class that are objects declared by other classes, the object created by the class can contain several other objects as its members. It is customary to refer to a member object in an object as its component, for example, the geometry (Geometry) class contains a triangle (Triangle) class, a rectangle (Rectangle ) class and Circle ( Circle ) class, the Geometry class can create a geometry that consists of triangles, rectangles, and circles, which are the components of the current geometry.

However, you may encounter the following difficulties in writing code for the construction method of a Geometry class:

(1) Some users do not need all the components contained in the object created by the Geometry class.

(2) Some users have special requirements for the components of the created geometry object, such as a user requires that the triangle is equilateral, the rectangle is a square, and so on.

Obviously, if an object is made up of many components, we cannot hard code in the construction method to meet the requirements of the various users on the structure of the component. Therefore, in the abstract-oriented principle, we should not do any coding in the Geometry class construction method, but instead divide the construction method of the geometry class object into several steps, that is to define several methods in one port according to the number of current components, and each method is responsible for creating a component of the geometry class object. The class that implements the interface is responsible for creating the Geometry class object, which means that the creation of the geometry class object is encapsulated in another class.

In design mode, the class that encapsulates the creation of an object is called the generator, as shown in the example above: (Where Buliderone and bulidertwo are generators)


Figure one: Creating a Geometry Class object with a generator

When the system is ready to provide users with an internally complex object, you can use the generator pattern, which allows you to construct objects incrementally, making objects more resilient to creation. The key to the builder pattern is to separate the creation of a multiple component object into several steps and encapsulate these steps in an interface called a generator.

Structure of the 1.2 pattern

There are four roles in the builder pattern structure:

(1) product : A complex object to be constructed by a specific production device;

(2) abstract builder (Bulider): Abstract Builder is an interface that defines several methods for creating individual components of a Product object, and defines a return The method of the Product object;

(3) specific production device (concreteproduct) : Implementing builder interface, the concrete generator will implement builder interface;

(4) conductor (director): The conductor is a class that requires a variable that contains a Builder Interface declaration. The role of the conductor is to provide the user with a specific generator, that is, the conductor will request a specific generator class to construct the product object required by the user , if the specific generator requested succeeds in constructing the product object, the conductor can let the specific producer return the constructed Product object.

The class diagram for the producer pattern structure is shown in two:

Figure two: Generator Pattern structure class diagram

1.3 Generator advantages of the model

(1) The builder pattern encapsulates the construction of an object into a specific generator, and the user can get different representations of the object using different concrete generators.

(2) The builder pattern separates the construction of an object from the class in which it was created, and is a specific component of the object that the user does not need to know about.

(3) The construction process of the object can be controlled more finely and effectively. The builder breaks down the construction of an object into several steps, which allows the program to be more granular and effectively control the construction of the entire object.

(4) The builder mode decouples the object's construction from the creation of the object class, making the creation of objects more flexible and resilient.

(5) when adding a new concrete generator, you do not have to modify the code of the conductor, that is, the pattern satisfies the open - close principle.

1.4 Fit using the generator pattern of the scene

(1) When the system is ready to provide users with a complex internal structure of the object, and in the construction method to write the code to create the object does not meet the user requirements, you can use the generator pattern to construct such an object.

(2) When some system requires that the object's construction process be independent of the class that created the object.

Design Patterns Learning Notes (12: Generator mode)

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.