Design Pattern structural pattern

Source: Internet
Author: User

In the previous blog, I wrote about the Creation Mode (factory family ). Here we will introduce the structural mode. It mainly describes the meaning, advantages and disadvantages, application scenarios, and structure of each mode to understand the structural mode.

The structural mode includes seven modes: adapter mode, appearance mode, proxy mode, Bridge Mode, shared element mode, combination mode, and decoration mode. Each mode has its own advantages and disadvantages. For more information, see the following.

1. Adapter Mode

Definition: converts an interface of a class to another interface that the customer wants. So that those classes that cannot work together due to interface incompatibility can work together.

Structure:

Applicable to: You want to use an existing class, but the interface and your requirements are different

In the later stage of software development or maintenance, it is not easy for both parties to modify

Advantage: allows different classes of interfaces to adapt and work collaboratively.

2. Appearance Mode

Definition: provides a consistent interface for a group of interfaces in the system. It defines a high-level interface, making this subsystem easier to use.

Structure:

Advantage: the customer is shielded from subsystem components, reducing the number of objects processed by the user and making the system easier to use.

Loose coupling between subsystems and users is realized,

Applicable to: When you want to provide a simple interface for a complex Subsystem

Separate Two layers to create an appearance between the layers.

3. Proxy Mode

Definition: provides a proxy for an object to control access to this object.

Structure:

Advantages:

Clear responsibilities. The real role is to implement the actual business logic. You do not need to worry about other transactions that charge fees. After completing a transaction through a later agent, the result is concise and clear.

Proxy objects can play a mediation role between the client and the target object.

High scalability.

Applicable:

Remote Proxy: provides a local representation of an object in different address spaces.

Virtual Proxy: more costly objects to be created.

Security Proxy: used to control the access permissions of real objects.

Intelligent Guidance: when calling a real object, the proxy handles other things.

Three:

 1. Agent VS appearance. The agent object represents a single object, and the appearance object represents a sub-system.

2. The client object of the proxy cannot directly access the target object.

You can directly access the objects in the sub-system.

3. Proxy is a representative of the original object, and other operations that need to deal with this object are negotiated with this representative.

The adapter does not need to create a representative. You only need to combine the original classes for specific purposes.

4. The appearance and adaptation are encapsulation of the existing system. The appearance defines a new system, and the adaptation is to reuse the original interface.


4. decoration mode:

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

Structure:


Advantages:

The purpose of the relationship between the decoration mode and the integration is to expand the object functions, but the decoration mode provides more flexibility,

By using different specific decorative classes and the arrangement and combination of these decorative classes, designers can create many different behavior combinations.

Disadvantage: more flexible features than inheritance also means increased complexity

This makes many classes appear in the design. Excessive use may complicate the program.

Decorator VS adapter, which was learned during query, is also shared here: 1. about new responsibilities: the adapter can also add new responsibilities during the conversion, but the main purpose is not here. The decorator mode adds new responsibilities to the decorator. 2. Original interface: the adapter mode uses a new interface to call the original interface. The original interface is invisible or unavailable to the new system. The original interface is used in the decoration mode, and the system also uses the original interface for the decoration objects. (The modifier mode for adding new interfaces can be considered as its variant-"Translucent" modifier) 3. about the object of the package: the adapter knows the details of the adapter (that is, the class or the interface ). The decorator only knows what its interface is. As for its specific type (whether it is a base class or other derived classes), it only knows during running. 5. Bridging Mode

Definition: separates abstract parts and implementation parts so that they can all change independently.

Structure:

If inheritance is used excessively, the structure of the class is too complex, difficult to maintain, and poor scalability. By decoupling these different changes, we can change the inheritance relationship into a combination so that the two are independent changes (that is, the principle of merging and aggregation multiplexing ).

6. Combination Mode

Definition: combines objects into a tree structure to represent the 'partial-all' hierarchy. The combination mode ensures consistency between users and composite objects.

Structure:

     

7. Metadata Mode

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

Structure:

Reading a book doesn't mean you really understand the mode. Learning to continue ing!

Related Article

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.