Design Mode Learning (3) single-piece mode-generator Mode

Source: Internet
Author: User

Single-piece Mode

To some extent, the single-piece mode restricts the creation of classes rather than improving the creation of classes, but it still belongs to the creation mode. The single-piece mode ensures that a class has only one instance and provides a global access point to access it. InProgramDuring the design process, there are many situations where you need to ensure that one class can only have one instance. For example, the task manager and the printer COM1 port.

Use static variables to implement the single-piece mode and design special exception classes. When a single-piece instance already exists, an exception is thrown.

Provides a single-piece Global Access Point:

1Create a single piece at the beginning of the program and pass it as a parameter to the class that needs to use it.

2Create a registry for all the single-piece classes in the program and make the Registry always available. Each time a single piece is instantiated, it is recorded in the registry. Any part of the program can use the identifier string to access any single-piece instance and retrieve the corresponding instance variables.

The most common way to provide a global access point is to use the static method of the class. The class name can always be used. The static method can only be called by the class and cannot be called by the class instance.

Generator Mode

Applicable scenarios: different user interfaces are displayed because of different data. For example, the e-mail address book contains both personal information and group information.

Understanding: based on different data, the display data interface is dynamically generated and displayed by specific classes.

The report system should be able to use the generator mode for design. Different types of reports are used to display different data and formats. A judgment generation class and a class with the same attributes of each report are required, one acceptance page, multiple report generation classes.

In fact, the generator mode is a bit like a page factory, which generates different page parts based on different data.

Effects of the generator mode:

1The generator allows the reader to change the internal representation of the product, while also hiding the details of how the product is assembled.

2Each specific generator is independent from other generators and is independent from other parts of the program. This improves the modularity of the object and makes it easier to add other generators.

3Since each generator builds the final result step by step based on the data, it can precisely control every result built by the generator.

The generator mode is somewhat similar to the abstract factory mode. Both return classes composed of many methods and objects. The main difference between them is that the abstract factory returns a series of related classes, and the generator is to build a complex object step by step based on the data provided to him.

Understanding: the abstract factory mode returns a series of complex related classes at a time. The generator mode is to judge data step by step and generate a complex object step by step. The actual data is generated based on the actual data.

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.