23 design patterns-Creation

Source: Internet
Author: User
Tags shallow copy

The 23 design patterns have been studied several times. There are too many parsing and examples on the Internet, so I am not using demo examples that consume a lot of space. In order to ** study this again, record the experience for future queries.

Note: The class chart cited the word Internet, but the source may not be unique, so not all sources will be listed.

======================================== Simple factory ================== ========================================================== ====================

1. Simple factory Mode

 

Solved problems:

1) Avoid using a large number of new descriptors in the program, because the name of the target class needs to be remembered during the new process (it is difficult for many classes ); in addition, if you want to change the name of a specific class for later maintenance, it will be very simple (concentrated in the factory class ).

2) Sometimes, when the parent class is used, it does not know the new subclass. In this case, the second model is used. For example, a method of a specific class is required for defining a parent class A, but the new class name should not be known, but the subclass B of A knows what object should be new. Therefore, a directly uses B to get specific classes. B is concretefactory.

Principle:

Simple factories generally have two main implementation methods,

Method 1 is common. The core is the type parameter in the createproduct (type) member function of the factory class. Different concreteproduct classes are created based on the type. The interface for object creation is declared, and the process for object creation is encapsulated.

The difference between method 2 and method 1 is that an abstraction layer is added, which not only encapsulates the creation of objects, but also places the creation process of specific classes into sub-classes for implementation, the parent class provides interfaces for creation.

Disadvantages:

1) When method 1 adds a new concreteproduct, it will change the createproduct () implementation, resulting in no closed. Method 2 every time a subclass is created, the concreteproduct class needs to be derived from the factory. The class explosion...

================================ Abstract Factory ========================== ========================================================== ==============================

2. Abstract Factory

 

Solved problems:

1) in daily programming, a group of similar objects is usually generated according to different situations. For example, A game can beat monsters by setting high school and low levels of difficulty (monsters are quite appropriate), so when it is set to different levels, the new object should be a response-level monster. Abstract Factory can solve this problem perfectly. This mode is used to create a group of related or interdependent objects.

2) It is easier to maintain, because you only need to derive a new factory class or add a new product class to add new objects in the same group.

Principle:

The implementation part of the abstract factory draws on the simple factory. The abstract factory class provides interfaces for generating a set of objects, and each specific factory completes the creation of a set of related objects. For example, concretefactory1 creates type 1 (A1 and B1) objects. Concretefactory2 creates 2 (A2 and B2) types.

================================= Single-instance mode ======================== ========================================================== ================================

Solved problems:

Objects in the system are globally unique, such as printers in the system and some factory objects in the system.

Principle:

There are two ways to implement lazy mode (Object creation is required) and hunger mode (new object during initialization). The core is to create a static private variable and a static mutual acquisition method, also, the constructor is private ..

Others:

The preceding Singleton mode can be used in combination with other modes, such as creating a unique factory object together with the factory mode. In addition, pay attention to the use of the singleton mode in multithreading. The Lazy mode is not multithread secure (check instance = NULL is required). The hungry mode is thread-safe.

================================= Builder mode ======================== ========================================================== ==============================

Solved problems:

1. to produce a product of the same type, these products have similar production process sequence (method), but each production process is slightly different from the builder model at the same time.

2. Produce different products based on different execution sequence of the same method

Key point: there are a series of identical methods. The Builder mode can be used when the execution sequence or implementation of these methods are slightly different.

Principle:

Product class: a specific product class, responsible for the product logic. Builder does not focus on the internal methods and logic of Product

Builer class: Two implementation methods, generally with two interfaces. Method 1: Provides method 1 for building the product's sequential structure e.g. builda () and buildb. Provides method 2 for getproduct () to return a specific product. In this way, you can build the product by calling different parameters in build * (para. This method is suitable for solving the problem of fixed production sequence. Method 2: setsequerence () sets the virtual method 1 for building the product sequence and getproduct () returns the virtual method 2 for the product. in this case, the same method is provided by the product class. Different concreteproducts implement these methods, and the building sequence is provided by the setsequence () method. For details, see reprinted blog: http://www.cnblogs.com/stephen-init/p/3915809.html

Concretebuilder: implements the builder Interface

Director class: contains a private builder interface data member and a construct () method. This method calls builder-> build * () to build and return the product object.

Others:

Note the differences with abstract factories. They solve different problems.

 

=============================== Prototype mode ======================== ========================================================== ==============================

Solved problems:

Sometimes you need to create the same object based on the current object, providing a higher level of abstraction than calling the copy constructor directly.

Key Point: copy the object yourself, pay attention to deep copy

Principle:

The mode is simple. Pay attention to the differences between deep copy and shallow copy.

Others:

N/

23 design patterns-Creation

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.