2004-12-1
Flyweight mode
Name: Enjoying Yuan Mode, Lightweight Mode, Feather Mode
problem:
It is of course very good to use objects, but assume a document editor to process text, you can look at all processed letters as objects (object-oriented), but this will generate huge amounts of objects, large system resources Consume. How to share these objects with the same feature?
solve:
Define a object called Flyweight, which is a shared object, which is used in different contexts (Context), which can be seen as individual objects. However, Flyweight does not make any assumptions on the context of it. The internal state exists in FlyWeight, which contains an opposite context, which can be shared; the external state depends on the context where the context is located, and the internal object is responsible for passing the external state to Flyweight at the time. Thus, there is a state in which all objects can be shared and each of the other objects. For example, Java's String object is the application of the enjoys.
effect:
It greatly reduces the number of objects and saves space. However, management is complex, and it is extremely careful to distinguish external state and internal state division.
Photo:
Proxy proxy mode
Name: Proxy mode, Proxy, Surrogate
problem:
Consider embedding images and other large elements in a editing system, creating these elements can consume resources, but we know that it is necessary to quickly, we should avoid creating all graphics at the time of opening, and may not Will see these graphics. This means that we are created as needed for an overhead, but how do this object in the documentation?
solve:
We designed a proxy object to represent large elements such as the diagram model, which is created and displayed during the death of this element. Proxy only represents information such as graphic storage when the document is continuous.
effect:
Coordinate the caller and the modified person makes the coupling decrease.
Photo:
There is a similarity between structural patterns, but in fact, the main difference is that different problems are different, and the use is different.
Adapter mode mainly solves the mismatch problems between the two existing interfaces.
Bridge mode bridges the abstract interface with his possible plurality of portions, allowing modification to implement this mode class
Composite and Decorator are similar to the recursive combination to generate new objects, but Decorator mode does not need to generate subclasses to add responsibilities to objects, avoid subclasses; and Composite is intended to construct classes, make multiple related formations Is a unified handling.