Design Pattern-structural pattern (including 7 types)

Source: Internet
Author: User

The Structural Design Mode solves the coupling problem between modules from the program structure. There are seven modes:

1. adapte Adapter mode: the Adapter mode focuses on converting existing interfaces through class inheritance or object combination. The class Adapter adopts the "Multi-inheritance" implementation method, resulting in poor high coupling, therefore, it is generally not recommended. The Object Adapter adopts the "Object combination" method, which is more compliant with the loose coupling spirit.

For example, the notebook power adapter can convert v to a voltage suitable for the notebook.

2. Bridge: separates the abstract part from the implementation part so that they can all change independently. Reduces the amount of code modifications caused by changes.

For example, in a typical example, an electric switch is used to turn the device on or off.

3. Composite combination mode: combines objects into a tree structure to represent a "part-whole" hierarchy. The Composite mode ensures consistency between the use of a single object and a Composite object. This solves the decoupling between customer programs and complex object containers. That is, by inheriting the unified interface, we can regard container objects and their sub-objects as the same class objects, to reduce the complexity of object usage.

For example, to allow users to use a single object and composite object in a consistent manner, 1 + 2 and (1 + 1) + (2*3) are legal expressions. The addition operator can be performed for both a single and a whole.

4. Decorator decoration mode: dynamically add some additional responsibilities to an object. The Decorator mode is more flexible than the subclass generation function. [GOF Design Pattern] the Decorator pattern uses Object combination instead of inheritance to realize the ability to dynamically expand object functions at runtime, and can expand multiple functions as needed, this avoids the "poor flexibility" and "Multi-subclass Derivative Problems" caused by the independent use of inheritance ". At the same time, it is well in line with the "preferential use of object combination rather than inheritance" and "open-closed" principles in the object-oriented design principles.

For example, a painting can be directly mounted to the wall, or added with frame and glass, and then mounted to the wall.

5. Facade appearance mode: provides a consistent interface for a group of interfaces in the subsystem to simplify the interface.

For example, when we call 10086, we can handle, ring tones, mobile phone reports, full-time and other services (sub-objects), while 10086 is the consistent interface used by sub-objects.

6. Flyweight: Use the sharing technology to effectively support a large number of fine-grained objects. [GOF design patterns]. Solution: the object-oriented approach solves abstraction issues well, and generally does not have performance problems. However, in some cases, the number of objects may be too large, resulting in the runtime cost. So how can we avoid a large number of fine-grained objects without affecting the use of object-oriented operations by customer programs? The emergence of the metadata mode exactly solves this problem.

For example, the Public switching Telephone Network (PSTN) is an example of the metadata. Some resources, such as the dial-up sound generator, the ring tone generator, and the dial-up receiver, must be shared by all users. When a user picks up a phone call, he does not need to know how many resources are used. All the things for a user are dialing, dialing, And dialing.

7. Proxy mode: provides a Proxy for other objects to control access to this object. Solve the Problem of directly accessing some objects.

For example, a lawyer is a proxy for our rights protection!


Summary: From the code perspective, the Adapter mode is similar to the Proxy mode. The former solves the problems encountered by existing objects in the new environment, the latter solves the problem of direct access to objects. Both modes solve the problem of direct access to objects in terms of usage, but the meaning is different.

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.