Design mode - structural mode

xiaoxiao2021-03-06  19

2004-08-30

Structural mode: Described is a series of combinations of classes and objects to build larger classes and objects. It emphasizes the structured way, not how to construct these classes.

Structural mode is divided into two: a, structure type mode: implementation through the inheritance of the interface or class. B, Structured Object Mode: Not a class or interface, but a combination of objects.

/ ************************************************** ************************************************************ / 1, Adapter (Adapter) / Wrapper The most important role of the packaging mode is to access the existing, but the interface is incompatible, but the interface is incompatible. Effective reuse of complex logic programs. The base class of Adaptor and Adaptee in this mode is unique.

A, the adaptation class adaptation is to adapt this static manner, that is, the adapter will inherit the thing that needs to be adapted. The disadvantage is that the scope of the adapter is relatively small, and it cannot meet the same set of objects. (Class and its subclass) requirements.

b, object adaptable object adaptation: The adapter does not inherit, and will be the way to adopt a category that needs to be adapted, so that the adaptation class and its subclasses can be adapted, disadvantages: need Dynamic specification is adapted, and it is easy to cause operational error

Note: It may be that other object-oriented languages ​​are not understood, I think it can be adapted to use objects. There is no need to use the mode adaptation mode. For only one case where you need adapter, we only need to reference dynamic constructs. The object's process can be added in the constructor of the adapter. After the other type of OO language is, it can be reviewed here again.

/ ************************************************** *************************************************************** / 2, Bridge (Bridge) / (Handle / boby) - The most important role of the structure of the structural object mode bridging is to separate abstraction / performance and implementation, and maintain a good scalability. Two interfaces of Window and WindowImpl in this mode are unique.

Design of a general inheritance mode WINDOW - XWindow PmWindow IconWindow - PmiconWindow XiconWindow This tree inheritance is integrated with abstract portions iconwindow and abstract implementation part (XWindow, PmWindow) together. And the two are responsible for different things, one is performance (external characteristics, if icon can be dragged, is a rectangular composition, etc.), the second is the specific implementation platform, responsible for how to draw in the xwindow style, There are so thick, from what position begins to paint.

So there should be two lines of structure: Window - IconWindow ApplicationWindow

WindowImpl - XWindow PmWindow

In the middle of the reference to WindowImpl, it is, that is, the mystery of bridging is here, reasonably separating the interfaces of the interface, is the key to bridge mode design.

The degradation form of bridge mode is that we just need to implement one of XWindow or PMWindow, so that WindowImpl loses its meaning. (For some languages, WindowImpl's presence may facilitate changes to achieve no need to recompile the client application)

/ ************************************************** ************************************************* / 3, Composite --- The structural object mode combination mode makes the use of a single object and a combined object consistency. Make the external interfaces becomes a single, and the user ignores the difference between a single object and the combined object. Combined models have effectively resolved such a contradiction: in a program group, it needs to be expressed (this is an internal reuse and readability issue), but the object in this program group has commonality, and wants external calling programs Ignore the partial part of it and just pay attention to its commonality (this is an external interface simple problem). So the relationship between its main elements is this: Composite (Composite Object) -Laf (Simple Object) Composite (Composite Object) -Laf (Simple Object) Leaf (Simple Object)

These objects achieve unified interface or abstract class: CompositeInterface; any client is performed by an interface for any object instance.

Mode implementation: There will be a series of problems in the implementation, which is only realized, and the model itself is independent. 1. Display the parent object reference: Solution: Create a stable, moderated parent child reference relationship and operation method. 2, shared component: This turns the relationship between the 1: N of the father and the child into the mode of N: N, refer to the Flyweight mode. 3, Maximize the Component interface: This is to ensure that the transparency is required, and we should try our best to achieve this. 4. Declare the management of child components: The biggest trouble of this problem comes from the maximum difference between the sub-component and the parent part - does it have a child node, can be done by unified, skillful add, and remove methods. 5. CompositeInterface should implement a list of compositeInterface: This is a problem with the storage, that is, the reference to the child node is meaningless memory consumption for the leaves node. 6, Submount Sort: Itrator mode should have a certain role. 7, cache improvement performance.

/ ************************************************** ************************************************************* / 4, Decorator / Packaging (wrapper) --- Structure object mode From the name, we can see the basic meaning of this mode: After a painter draws a picture (implementing a component), it does not need to do itself to do the picture (decorator). Do this picture is the problem that this mode needs to be solved. It can be seen that the picture frame can be used for the painter, which is based on the premise of the painter's paper and picture frame. It is based on a result, which is painted, not painted because of the picture frame. The essence and content, just now a boxed painting. Back in the mode, we can see this mode probably:

Component -concreteComponent (component implementation) -Decorator (decorator) -> m_cmp (reference to component implementation)

Here we can see the difference between Decorator mode and Adaptor mode, Decorator focuses on reuse of the decorator part, and Adaptor is just a reuse of components. Decorator focuses on package assembly that can be inserted, its focus is a functional reuse of component levels. Adaptor is eye-catching or reuse of the component level.

/ ************************************************** ****************************** In the previous Composite and Decorator mode, We have pursued the components more finely grained in order to increase the reusability of components, which is basically a common feature of a variety of well-designed. But this design can also lead to various issues, including scattered, and resource consumption problems caused by a large number of object instances. / ************************************************** *************************************************** / 5, FACADE (appearance) The maximum feature of Type Object Mode FACADE mode is to make a unique entrance package that will be complex, and scattered subsystems. This mode is the most impressive in the system I experience is the interface between the multilayer layers, a unique entrance, which makes the hierarchical clear. In fact, this mode is a certain similarity to Adaptor, which is shielded two different subsystems, but different is that facade is active construction, and Adaptor is built to access other systems, It seems that you want to build Adaptor for a subsystem that has already implemented a Facade mode, it is much simpler than if there is no FACADE mode, the cost is much smaller.

/ ************************************************** ************************************************************ / 6, Flyweight (Enjoy Yuan) - Structure object mode Enjoyment dollar design mode is resolved is that the resource consumption problem caused by a large number of object instances in memory. Several aspects can be understood that the enjoyment of the element is pursuing the reuse of the instance level. Separate the external state (context) and the object instance itself, not in the construction of the external state, but during the running period, even this state is completely process, and the object instance has no relationship. The internal state is only specified when it is created, and during the running period is absolutely unable to touch.

The biggest difference between this mode and prototype mode is that a shared instance level, one is the level of shared class.

/ ************************************************** ************************************************* / 6, Proxy (agent) / surrogate- - Structured object mode common usage 1, remote agent (Virtual Proxy): Create a large number of overheads as needed. 3, Protection Proxy: Controls access to the original object. 4, Smart Refrence: Replace the simple pointer to facilitate do some additional operations before accessing the object. This agent can be implemented in such a way with a wide range of techniques, what COPY_ON_WRITE, LAZY_LOADING can consider this approach.

The Proxy mode agent and the proxy object interface are consistent or subset, but the Adaptor mode is different. Decorator mode is an object to increase the interface, and Proxy is forwarding or restricting an interface.

转载请注明原文地址:https://www.9cbs.com/read-46374.html

New Post(0)