Design Patterns Summary 2-Structural Patterns

Source: Internet
Author: User

The structure mode discusses the structure of classes and objects. It uses an Inheritance Mechanism to combine interfaces or implementations (class structure mode), or by combining some objects, to implement new functions (object structure mode ). In gof23, there are 7 structural patterns. The specific categories are as follows:

  • Adapter Mode

Convert an interface to another interface that the customer wants. The adapter mode allows the classes that cannot work together due to the incompatibility of the interface (that is, after different classes of the interface pass the adapter, ).

 

  • Bridging Mode:

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

In the decoupling method, aggregate is used instead of inheritance. Although inheritance is a good thing, the transitional application will lead to the complexity of the class structure, too many relationships, and difficult to maintain. What's worse is that the scalability is very poor. If we find that there are two or more changes in the inheritance system, we can decouple these changes in different directions for a long time. Through object combination, the inheritance relationship between the two roles is changed to the combination relationship, so that the two can change independently. Find and encapsulate the changes.

 

  • Combination Mode:

The object is combined into a tree structure to represent the 'partial-all' hierarchy. The combination mode ensures consistency between the use of a single object and a composite object.

 

  • Decoration mode:

Dynamically add some additional responsibilities to an object. As for the added function, the decoration mode is more flexible than the subclass generation.

In the face of changes, if the inheritance method is used, a large number of child classes will be generated, resulting in explosive growth in the number of child classes. In fact, most of these subclasses only add some responsibilities to an object. In this case, they can be decorated in a more flexible, dynamic, and transparent way to add responsibilities to a single object, revoke responsibilities.

 

  • Appearance mode:

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

Information Hiding promotes the reuse of software. In the appearance mode, an appearance object is introduced to minimize the communication and dependency relationships between subsystems in the software, it also provides a single and simple barrier between subsystems.

 

  • Metadata mode:

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

 

  • Proxy mode:

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

 

  • Proxy mode and appearance mode:

The proxy object represents a single object, and the appearance object represents a sub-system. The proxy client objects cannot access the target objects, and a proxy provides access control over the individual target objects, the customer object of the appearance can directly access each object in the subsystem, but the appearance Object usually provides a simple and common call to the functions of the sub-system components.

  • Proxy mode and adapter mode:

Both of them belong to a cohesive function, and the proxy represents an object. Other operations that need to deal with this object are negotiated with this representative. The adapter does not need to create a representative. It only needs to combine the original classes for specific purposes.

 

In short, the appearance mode focuses on simplified interfaces, the adapter mode focuses on the conversion interface, the Bridge Mode focuses on the separation of interfaces, and the decoration mode focuses on object extension functions under the premise of stable interfaces.

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.