Design Mode Study note 13-structural mode Summary

Source: Internet
Author: User
Learning the creation mode gradually understands the advantages of interface-Oriented Programming and implements the principle of dependency abstraction. Learning the structure mode shows the benefits of using object combinations. Compared with inheritance, it brings many advantages, especially in the bridge and decorator modes. In fact, the structural model can be seen everywhere in the project, and is used unconsciously in the previous project. Although we do not know it before, it is the model. Of course, many times, I have not abstracted and refined those objects, and it is still strongly coupled.
The model is a bit similar. It is nothing more than separating the responsibilities of the class and implementing complicated functions through the combination of two categories. These two parts are often changed independently, by isolating them, it is natural to bring benefits-to better face the challenges of changes. If inheritance is used, it is very difficult to face changes in multiple aspects or directions.
1. adapter, bridge, and facade
An object provides a certain degree of indirect property and forwards requests to them through other object interfaces. In fact, it is to isolate the objects that receive and process requests. The objects that accept the request are processed and handed over to the objects that process the request. They can all change independently.
The difference is that, for different purposes, the adapter solves the problem of interface incompatibility, which allows classes with incompatible interfaces to work collaboratively without considering the implementation of these interfaces and the evolution trend of these classes, it is equivalent to a converter that forwards requests. The Bridge Mode solves the problem of abstraction and independent changes. It isolates abstraction and implementation and bridges these two parts so that they can be changed independently. The abstract part has a stable interface, which is implemented by the implementation part.
Adapter and bridge modes are usually used in different software lifecycle stages. Adapeter is a remedy. When two classes are incompatible and must work together, use it for transfer. It is not foreseeable before coupling. When using the bridge mode, you must know that an abstract has multiple implementation parts and the two parts are evolved independently. Without this requirement, do not use the bridge mode. The adapter mode is implemented after the class design, while the bridge mode is implemented before the class design.
The difference between an adapter and a facade is that the facade mode is not a set of adapters, that is, it is not an adapter of a group of objects. The adapter reuses the previous interface, while the facade mode re-defines an interface and uses some sub-objects to operate on the interface provided externally. The adapter allows two existing interfaces to work together, instead of defining new interfaces.
2. composite, decorator, and proxy
Both composite and decorator modes organize variable target objects based on Recursive combinations, but their purposes are completely different. Composite unifies a single object and an interface for combining objects to make it transparent to the outside world. Program I don't need to know any details about them. When a single object is so consistent with a composite object, it focuses on representation, that is, how to represent a single object and a composite object, and how to use it with consistency? The decorator mode solves the problem of object function expansion. It isolates the changes of the object and its extension changes, making the new object extension function so simple, it eliminates the problem of expanding sub-classes for multiple features of multiple sub-objects, and also provides the possibility of dynamically expanding object functions during runtime, which cannot be achieved through simple inheritance. Ideologically, it is similar to the bridge model.
Both decorator and proxy reference other objects and forward requests to this object. However, they have different design purposes. Proxy cannot dynamically add or delete extension functions of objects, nor is it designed for Recursive combination. It is used to provide a proxy object for the object when it is not convenient to access an object and does not meet the requirements. The proxy object forwards requests to it for processing, such as remote access. In the proxy mode, the entity object implements the key functions to be accessed. The proxy object provides or rejects access to the object. In the decorator mode, A component is a subject object that only provides partial or basic functions. One or more decorator objects implement other functions. Decorator is suitable for situations where all of its functions cannot be determined during compilation. You can dynamically add or delete functions at runtime.

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.