Structured Mode C #

Source: Internet
Author: User

Structural

Adapter

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.

 

The object-oriented spirit is to better cope with changes in requirements. In reality, there are often the following situations: to use an existing class, and its interfaces do not meet the requirements, or you want to create a reusable class that can work together with other unrelated or unpredictable classes. Follow the "Disable modification and open extension" principle to enable different classes of these interfaces to work together through the adapter.

 

The adapter mode is mainly used to solve the mismatch between two existing interfaces. It does not need to consider how these interfaces are implemented or how they may evolve. This approach allows them to work collaboratively without re-designing any of the two independently designed classes.

Bridging

Separates abstract parts from their implementations so that they can change independently.

 

Inheritance is a good thing, but it is often used excessively. inheritance will lead to the complex structure of the class, too many relationships, and difficult to maintain. What's worse is that the scalability is very poor. If we can find that there are two or more changes in the inheritance system, we can decouple these changes in different directions. Through object combination, the inheritance relationship between the two roles is changed to the combination relationship, so that the two can cope with their independent changes, follow the principle of merging/aggregation reuse, identify and encapsulate the changes.

 

Bridging has something in common with the adapter, that is, to provide a certain degree of indirect property to another object, which facilitates the flexibility of the system. However, the bridge is usually used to bridge the abstract interface and its implementation at the beginning of the design, so that the abstraction and implementation can evolve independently.

Bridging and decoration are applied to different stages of the software life cycle, which are aimed at different problems.

 

Appearance (dimit rule)

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. The weaker the coupling between classes, the more conducive to reuse. A class with weak coupling is modified and will not affect related classes. If the two classes do not need to communicate with each other directly, do not let them interact directly. If you need to call the API, you can use a third party to forward the API call.

 

The communication and interdependence between subsystems in a software should be minimized. The specific method is to introduce an external object, which provides a single and simple barrier between subsystems.

 

The appearance is similar to that of the adapter, Which is encapsulation of the existing system. The external interface defines a new interface, while the adapter reuses the original interface. The adapter uses two original interfaces to work collaboratively, the appearance is to provide a more convenient access interface for the existing system. An adapter is used to adapt to objects, while an external adapter is used to adapt to the entire subsystem. The external adapter is more specific to objects.

 

Combination

The object is combined into a tree structure to represent the "part-whole" hierarchy. The combination mode ensures consistency between the use of a single object and a combination object.

 

The user uses a combination interface to interact with objects in the combination structure. If the receiver is a leaf node, the request is processed directly. If the receiver is a combination object, the request is usually sent to its child parts, some auxiliary operations may be performed before or after the request is forwarded. The effect of the combination mode is that the customer can use the combination structure and a single object in a consistent manner. Composite objects can be used wherever basic objects are used.

 

Decoration (synthesis/aggregation multiplexing)

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 subclass generation method is used for expansion, in order to support the combination of each extension, a large number of subclasses will be generated, resulting in explosive growth in the number of subclasses, this is a disaster brought about by inheritance. In fact, most of these sub-classes only add some responsibilities to an object. In this case, they can be more flexible through decoration, add responsibilities to a single object in a dynamic and transparent manner, and revoke the corresponding responsibilities when not needed.

 

Share (single responsibility)

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

 

Objects make the memory overhead too large, and if they are a large number of repeated objects, it is a great waste of resources, which will slow down the machine performance. This is obviously not feasible.

 

Object-oriented technology is sometimes costly for simplified design. The metadata mode avoids the overhead of a large number of similar classes. In program design, a large number of fine-grained class instances are sometimes required to represent data. If we can find that these instances are basically the same except several parameters, sometimes the number of classes to be instantiated can be greatly reduced. If you can move those parameters out of the class instance and pass them in when calling the method, you can greatly reduce the number of individual instances by sharing them.

 

Agent (dumit)

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

 

Proxy and appearance differences:

The proxy object represents a single object, and the appearance object represents a sub-system. The proxy client object cannot directly access the target object, and the proxy provides access control over the individual target object, the appearance of the customer object can direct the orientation of each object in the sub-system, but usually the appearance object provides a simplified and common interface for calling the functions of each component of the sub-system.

 

Proxy and adapter differences:

Proxy is a representative of the original 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.

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.