Creation Mode of design mode (I)

Source: Internet
Author: User

Learning without summarization is not counted as learning. This month's learning has gained a lot. Let's share it with you.

I. Classification of design patterns

In general, the design model is divided into three categories:

1. There are five creation modes.

2. There are seven structural modes.

3. 11 behavior patterns in total.


First ResearchCreation Mode

Ii. Overview

The creation mode is the mode used to create objects and abstracts the instantiation process. It helps a single system <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + branch + y/branch + tdrSu6Osy/branch 0Km + branch + o6zL/branch + Branch/ao6y2 + LK7x + Wz/sbkvt/M5bXEyrXP1s + Branch/Branch + samples/gudi78s/samples + samples/ztcTKtcD9u6 + yv7fWs + nIocHLs/samples/By8 + samples/samples + sa3oaM8L3A + samples + s + samples /users/uPbG + 7O1uavLvr/JxNzSqs2syrHJ + rL6vc6ztaOsu/WztaOsv82ztaOsxMfDtMO/users + users = "left"> Intention: Separates a complex build from its representation so that different representations can be created during the same build process.

Applicability:

· When creating complex objects, algorithms should be independent of the components of the objects and their assembly methods.

· When the constructor must allow different representations of the constructed object.

Comparison with the abstract factory Model

The builder mode focuses on separating the process of constructing an object from the parts of the structure, while the abstract factory focuses on building a product series. In fact, the biggest difference is that the products created in the builder mode do not necessarily have a common parent class, as long as there is a similar construction process.

The builder mode is very similar to the factory mode, but they have obvious differences. That is, the factory mode only returns different objects according to the given parameters. The builder mode not only produces different objects according to different parameters, but also contains different data. The builder model is more complex than the factory model in the "data" section.

3.Factory method mode

Intention: Defines an interface for users to create objects, so that the subclass decides which class to instantiate. The factory method mode delays the instantiation of a class to its subclass.

·Advantages: The principle of opening and closing is realized, and new products can be added without changing the factory.

·Disadvantages: It is reflected in its factory class. Because the factory class has centralized the creation logic of all instances, it is not easy to do "high cohesion. In addition, when the number of product categories in the system increases, the factory category may be required to be modified accordingly, and the scalability is not very good.

Applicability:

· When a class does not know the object it must create.

· When a class wants its subclass to specify the object it creates.

· When a class delegates the responsibility of creating an object to one of multiple help sub-classes, and you want to localize the information of which help sub-classes are proxies.

3. prototype mode

Intention: Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object.

Advantages: After the Prototype mode is introduced, a factory method class that is parallel to the specific product level structure is no longer required, reducing the class structure, at the same time, the customer program can create and delete prototypes at runtime (this is especially important when the user-defined interface is used ).

Applicability:

· When a system should be independent from its product creation, composition, and representation;

· When the class to be instantiated is specified at the runtime, for example, through dynamic loading;

· To avoid creating a factory class level parallel to the product class level;

· When an instance of a class can only have one of several combinations of different States. Creating prototype and cloning them may be more convenient than manually instantiating this type with the appropriate state each time.

Comparison:

The Prototype mode is the same as the factory mode. It also hides the object creation work for the customer. However, unlike constructing a new object by instantiating a class, the prototype creates a new object by copying an existing object to isolate the coupling relationship between the user of the class object and the specific type (variable type.

5. Singleton Mode

Intention: The single-piece mode ensures that the application has only one globally unique instance and provides a global access point to access it.

Structure: This includes private constructor that prevents other objects from creating instances, private variables of unique instances, and global access interfaces.

Comparison:

· Comparison with global variables: the single-piece mode maintains its own instantiation, which is safe for use. If a global object cannot be maintained by itself, it cannot avoid repeated creation of multiple instances, and system resources will be greatly occupied.

Even worse, there may be logic problems in many cases. When these objects access the same resources (such as serial ports), access conflicts may occur.

· Comparison with static methods of classification: The classification class provides static methods that are common to the system and often uses private constructors. Unlike a single piece, it does not have an instance, and all the methods are static methods.
(1) The lifecycle class does not save the status and only provides functions.
(2) The category class does not have polymorphism, but a single piece can have subclasses.
(3) A single piece is an object, and the category class is only a collection of methods.

Advantages:

· Instance control: Singleton prevents other objects from instantiating copies of its own Singleton object, so that all objects can access a unique instance.

· Flexibility: Because classes control the instantiation process, classes can flexibly modify the instantiation process.

Applicability:

· When a class can only have one instance and the customer can access it from a well-known access point.

· When this unique instance should be extensible through subclass, and the customer should be able to use an extended instance without changing the code.

Feeling: one month of study, while I was amazed at the magic of the design model, I realized that learning really comes from the point of view. When I first came into contact with this book, I saw such a thick textbook and had some conflict with it. However, after studying this book for a while, I would take the initiative to contact it. Learning is not terrible. What's terrible is that your mentality is incorrect.

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.