Intermediary mode
Name: Mediator, Turpler mode
problem:
Object-oriented object-oriented objects to many independent objects can enhance multiplexing, but the interaction between the objects has brought relevant reducing multiplexability (contradictory confrontation). Suppose the following scenarios: One input box, button linkage dialog, enter a value, other selection buttons should be unavailable, on the other hand, if a selection button is selected, the value other than this range should be entered. Different dialogs are (above) different dependencies, must customize dialog assemblies to reflect this dependency, and there are many classes (objects) involved, and if the method of generating subclasses will result in lengthy.
solve:
Package the collective behavior in one intermediator, it is responsible for controlling, coordinating the linkage, dependence relationship between the objects, making the connection between the objects through the intermediaries, only knows that there are intermediaries without knowing other objects, reducing coupling.
effect:
Reduce the number of sub-objects, reduce coupling, simplifying the same protocol, how to abstain between objects. The attention can be made from the object to the interaction between the object. But it will make several objects difficult to maintain (this seems to be complexity in nature).
Photo:
Memo mode
Name: Memo Mode, Snapshot, Token Mode
problem:
Typical questions how to support UNDO class operations. For example: a graphic editing system requires a record of the operation of the graphic object. In general, we use a system called Constrainsolver to explain the mutual constraint relationship between the objects. However, the Constrainsolver interface is not enough to accurately reverse it to other new roles, and we cannot expose the inside of Constrainsolver to the cancel operation mechanism.
solve:
The cancel operation mechanism will request a memo to the source generator when the inspection point of the source is set, and the cancel operation mechanism requests a memo to the source generator. This memo, only the source generator can access memo. Our graphic editing system can solve this:
When moving operation, the editor requests a memo to Constrainsolver;
Constrainsolver storage internal status to the memo object, return to the editor;
When you need to cancel your operation, the editor will the original memo to the Constrainsolver object, which is restored by the latter.
effect:
Keep the encapsulation boundary, conceal the internal information of the complex source of source to other objects; simplify the source generator; but the price may be high (consuming memory / co-deposit)
Photo:
Observer mode
Name: Observer, publish / subscription mode, model / view (model / view) mode, source / listener mode (Source / Listener), dependents / dependency mode
problem:
Many applications are separated from the performance layer to multiplex data objects and performance objects. Once the data changes, the performance layer should change, for example, may need to change, and the sector map also needs to be changed. We don't have to limit the objects that will be affected, which can be multiple. How to achieve?
solve:
Observer mode resolves this issue. As the target, other performance layer objects are observer, once data changes, other observes are notified and respond. Each observer must query the status of the target and synchronize. (Specific view) Effect:
The target is an abstract coupling between the observer to support broadcast communication (all observer knows the notice). But the simple update of the target may cause huge changes to the observer.
Photo:
Status mode
Name: Status mode, Status Object Mode (Objects for State)
problem:
Consider a plurality of states of TCP linkages (connected, listening, closed, positive link), etc., how to reflect when receiving other objects? This uses the status mode.
solve:
Use the TCPState abstraction to represent the state of the connection, declare the operation of the status, and then implement specific status and corresponding operations by subclasses of the state. The TCP link object maintains a state class instance to deliver all received requests to the state class instance. This once the state changes, the TCP link class changes the specific status class instance to respond for targeted.
effect:
Localize the operation related to the state. Make the sleeve conversion display. The status object can be shared.
Photo:
Policy mode
Name: Policy Mode, Policy Mode (Policy)
problem:
Considering the price calculation in a shopping system, simple is that the unit price is multiplied, but there may be different discounts (such as some 3% discount, some no discount), and the discount may continue to change over time, how to implement it?
solve:
Separate environment and discount behavior, environmental class is responsible for maintaining and querying behavioral classes, and various algorithms are specifically implemented in an algorithm class. A new discount only needs to be implemented in the policy class.
effect
Provides reusable algorithm function; provides a method of replacing inheritance (without inheriting environmental classes, only need to be modified, new policy classes). Eliminate complex conditional statements in the design selection statement. But increase the number of objects, increasing the communication overhead between the strategy class with environmental class, and the customer is forced to know the strategy class.
Photo:
Template mode mode
Name: Template Mode Mode
problem:
Consider an application framework for providing Application and Document classes, Application opens the document with OpenDocument. But there are many Document types and cannot be implemented in a document.
solve:
Different OpenDocument as a template method, define a method with some abstract operations, and subclasses redefine these methods. (Subcarphere herein refers to the subclass of Application and Document)
effect:
Code multiplexing, extract public behavior in the class library. Reverse control (method of manipulating subclass of parent class)
The template class is extremely common, and almost all frameworks have an application example.
Photo:
Visitor mode
Name: Visitor mode
problem:
Consider a compilation system to view the source program as an abstract syntax tree, the compiler needs to analyze the operation in this tree, mostly require different operations for different nodes, but different operations are dispersed into different nodes. .
solve:
Pack each class's related operations to the Independent Object (Vistor), passed this object to the node element being accessed during the abstract syntax tree. This element emits an operation request containing its own class information to the visitor, and performs an element as a parameter to perform operations for an element by the visitor.
effect:
Make the new operation, focusing on the relevant operational separation class is independent. However, it is difficult to add elements. Access can be performed by a class level. At the same time, the cumulative state is generated, and the visitor may destroy the internal state of the element, destroy the package. Photo: