Design Pattern 1

Source: Internet
Author: User
I. Structural Mode

The Structural Design Mode solves the coupling problem between modules from the structure of the Program (it seems like a nonsense). In the gof23 design modes, there are 7 structural design modes: adapter adapter, bridge, composite, decorator, facade, flyweight, and proxy modes. The following describes the modes:

Design Mode

Gof description

My understanding

Adapter Mode

Converts an interface of a class to another interface that the customer wants. The adapter mode allows classes that cannot work together due to interface incompatibility to work together.

Due to changes in the application environment, you often need to place "some existing objects" in the new environment. However, the interfaces required by the new environment are not met by existing objects, in this case, we can add some interfaces to the existing object by adding an adapter to adapt it to the new application environment.

Bridge Mode

Separate the abstract part from the implementation part so that they can change independently.

Separation interface (abstraction) and its implementation. When a type has two or more latitude changes (or change points), the changes are isolated by inheriting the interface, to reduce the amount of code modifications caused by changes.

Composite Combination Mode

Combine objects into a tree structure to represent the "part-whole" hierarchy. The composite mode ensures consistency between the use of a single object and a composite object.

Solve the decoupling between customer programs and complex object containers. A class of objects with "Container characteristics" -- that is, they act as objects and are containers of other objects, by inheriting the unified interface, we can regard the container object and its sub-objects as the same class object to reduce the complexity of object usage.

Decorator decoration Mode

Dynamically add some additional responsibilities to an object. The decorator mode is more flexible than the subclass generation function.

The object extension function is implemented on the premise of a stable interface, mainly to solve the problem of expanding the number of sub-objects caused by the extension of a large number of functions for the object through inheritance.

Facade appearance Mode

Provides a consistent interface for a group of interfaces in the subsystem. The facade mode defines a high-level interface, which makes the subsystem easier to use.

A simplified interface that encapsulates complex subsystem or sub-object calls. From the perspective of the customer program, you can only see the interfaces provided by facade. In other words, it is the encapsulation of sub-object calls, separating the calls of sub-objects of the client program from the changes of sub-objects.

Flyweight meta Mode

Use sharing technology to effectively support a large number of fine-grained objects

It mainly solves the problem that the system memory overhead is too large due to the large number of identical objects. Actually, the same object reference points to the same object space. Make an assessment before using the flyweight mode. improper use may be counterproductive.

Proxy Mode

Provides a proxy for other objects to control access to this object.

Solve the Problem of directly accessing some objects, such as accessing remote objects.

In the learning process, the adapter mode is similar to the proxy mode from the code perspective. The adapter mode solves the shortcomings of existing objects in the new environment, the proxy mode solves the problem of direct object access. Both modes solve the problem of direct object access from the perspective of usage, but the meaning is not very same.

Ii. Creation Mode

Five creation modes are available in gof23 design modes: Singleton single-piece mode, abstract factory Abstract Factory mode, Builder mode, factory method mode, and prototype original mode. The following describes the design modes.

 

Design Mode

Gof description

My understanding

Singleton single-piece Mode

Ensure that a class has only one instance and provides a global access point for the instance

Control the number of object objects

Abstract Factory Mode

Provides an interface for this interface to create a series of "related or interdependent objects" without specifying their specific classes.

Solve the Problem of object changes in a series

Builder Mode

Separates the construction of a complex object from its representation so that different representations can be created during the same construction process.

Create complex objects in the project. The so-called "complex object" refers to: This object also contains other sub-objects

Factory method factory method mode

Defines an interface for creating objects, so that the subclass decides to instantiate the class. Factorymethod delays the instantiation of a class to the subclass

The solution is to create an object. due to changes in requirements, this object is often subject to drastic changes, but the interfaces of this object are relatively stable. That is to say, branches often change, but branches and trunk interfaces are relatively stable.

Prototype original mode

Use the prototype instance to specify the object type, and then copy the prototype to create a new object.

The creation of some complex objects; due to changes in requirements, these objects often face drastic changes, but they have stable and consistent interfaces.

In the course of learning, the abstract factory mode and builder mode are easy to confuse in terms of functions. The factory method mode and prototype mode are difficult to distinguish. The Singleton mode does not have much difficulty. The following describes the first four modes.

I. Abstract Factory mode and builder mode:

Abstract Factory is used to create a series of objects. As an example in the previous article, abstract factory is more focused on creating a series of objects for creating a car object, or it is the creation of various parts of the automobile type, such as: wheel, engine, body, and so on. In other words, the focus is on this series of objects.

Builder is used to create a complex object, or to create sub-objects in the complex object. In the example of a car, I think the builder mode pays more attention to the car type than the abstract factory mode (the "complex object" mentioned above ") and the creation of various parts (such as wheel, engine, and body. The builder mode requires that the integration of various child types in this complex type (automobile) is relatively stable. The example shows that for an automobile, No matter what accessories are assembled, each accessory is assembled in the same way and has a relatively stable interface. The engine of the brand or brand of the car you use may change very frequently.

Ii. Factory method mode and prototype mode:

At first, I thought the two modes were the same in terms of functionality (personal opinion), both of which encapsulated the creation of objects, however, the prototype mode uses prototype clone to copy objects. Note the differences between the copy and copy modes. After consulting with colleagues, I understood it. The two models are different in application scenarios.

The factory method mode is to recreate an object.

The prototype mode clones existing objects. When two or more objects are the same, you can use a created object to clone other objects.
The above is a summary of the Creation Mode. If you have any incorrect ideas, please correct them.

Design Pattern 1

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.