Design pattern Learning-builder mode (builder mode)

Source: Internet
Author: User
Tags abstract

All supporting codes are on GitHub: Https://github.com/ZHONGHuanGit/DesignPattern

Brief introduction:

The builder pattern is also called the generator pattern, defined as: encapsulates the construction process of a product and constructs it by this step.

The builder pattern (builder pattern) belongs to the Create class pattern, and you'll feel a bit like it, but there's a difference, compared to the factory pattern. However, it is recommended that before you look at the three factory model, if you remember not very clear, you can see here, simple factory, factory method, abstract factory.

UML Class Diagram:

Take a look at the UML class diagram. This part, if not clear, can first look at the scene section below, and then look back.

From the class diagram above, you can see that there are four elements.

1 abstractbuilder (abstract builder): The purpose of introducing the abstract builder is to make the concrete process of construction into its subclasses. This makes it easier to expand. There are generally two parts of abstract methods, one for building products and the other for returning products. As above, BuildPart1 and BuildPart2 are used to construct products and Retrieveresult return products.

2 ConcreteBuilder (Concrete builders): Abstract methods for the realization of abstract builders, so as to facilitate expansion in different situations.

3 Director (director): responsible for calling the appropriate builders to build products, the Director class does not typically have a dependency on the product class, and direct interaction with the Director class is the Builder class. In general, the Director class is used to encapsulate variable parts of the program.

4 Products (Product Class): is generally a more complex object, that is, the process of creating objects is more complex, there will generally be more code. In this class diagram, the product class is a concrete class, not an abstract class. In practical programming, a product class can consist of an abstract class and its different implementations, or it can be composed of multiple abstract classes and their implementations.

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.