IOS Design Pattern Learning (6) Generator

Source: Internet
Author: User

1 Preface
Sometimes there are multiple ways to build some objects. If these logics are included in a single method of the class that builds these objects, the building logic will be ridiculous (for example, A large nested if-else or switch-case statement for various build requirements ). If the building process can be broken down into the relationship between client-ctor-builder, the process will be easier to manage and reuse. The design pattern for this type of relationship becomes a generator.

2. Details
2.1 Brief Introduction
In addition to the customer and the products required, the Builder mode contains the following roles: Director and Builder ). The Builder knows how to build a feature product without specific information. Director knows how the Builder should build and uses parameters to provide it with missing information to build a specific product.

The builder mode separates the construction of a complex object from its performance, so that the same building process can create different manifestations.

2.2 When to use
(1) complex objects involving various components must be created. The algorithm for creating objects should be independent of the assembly method of parts. A common example is to build a composite object.

(2) The build process requires building objects in different ways (for example, components or different combinations of performance.

2.3 comparison between generators and abstract factories
(1) generators construct complex objects and abstract factories to construct simple or complex objects;

(2) The generator constructs objects in multiple steps, and the abstract factory constructs objects in a single step;

(3) generators build objects in multiple ways, abstract factories build objects in a single step;

(4) The generator returns the product at the last step of the build process, and the abstract factory immediately returns the product;

(5) The generator focuses on a specific product, and the abstract factory emphasizes a set of products.

3 conclusion
The builder mode helps you build objects that involve components and representations.

The above is all content and I hope it will help you.

 

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.