The third major type of design pattern-behavior pattern, the following is the Observer mode, template method mode, command mode, State mode, responsibility chain mode of the five after the review, Welcome to Exchange!
Observer mode (Observer): defines a one-to-many dependency between objects, and when an object's state changes, all objects that depend on it are notified and actively updated by themselves. [ Liar design mode ]
features: similar to object-oriented polymorphism, only the object-oriented polymorphism is that the same object behaves differently at different times and under different conditions, whereas the observer pattern polymorphism emphasizes that the different objects caused by an operation (or command) react at the same time, the reaction can be the same, Can be different, can show different functions, show different phenomena.
use: changes to an object need to change other objects at the same time.
assistance: to solve the problem of "multi-state of the observer pattern" by means of the different names, unable to unify the action.
Template method Mode: defines an algorithm skeleton for an operation, and delays some steps into subclasses, The template method enables subclasses to not change the structure of an algorithm and to define certain steps of the algorithm. [ Liar design mode ]
Features: enables the reuse of large amounts of code, rather than replication. This ensures the consistency of the instances under the template objectively, and provides convenience in the later maintenance. The meaning of object-oriented inheritance is the same.
use: when we want to complete a process or a series of steps that are consistent at a certain level of detail, but whose individual steps are implemented at a more specific level may not be the same at the same time, we usually consider using the template method pattern.
Command mode: encapsulates a request as an object, allowing you to use different requests to count the client, the ability to queue or log request logs, and to support revocable operations. [ Liar design mode ]
Features: ability to queue requests and process commands sequentially, and also supports undo operations.
Tip: The Agile development principles tell us not to add code-based features that are not actually required to be pushed.
use: If you do not know whether a system requires command mode, generally do not rush to practice it, in fact, in the need to achieve this mode by refactoring is not difficult, only in the real need such as undo / redo operations and other functions, Refactoring the original code into a command pattern makes sense. [ Liar design mode ]
status mode (State): when an object's internal state changes and agrees to change its behavior, the object looks like it has changed its class.
feature: the state mode reduces the dependence of each other by distributing the various state transitions to the sub-classes of States.
hint: in the use of the process, more use of the inference statement, which requires that these inference statements to the state of the changes into stages, it should be noted that in the scope of the consideration should be considered complete, can not be omitted or overlapping, to avoid errors occur. Assuming that the actual result of the split, it is best to join the use of error-guided statement avoidance.
use: When an object's behavior depends on its state, and it must change its behavior based on state at execution time, it can consider using state mode.
Responsibility chain mode (Chain of Responsibility): enables multiple objects to have the opportunity to process requests, thus avoiding the coupling between the sender and receiver of the request. Link the objects together and pass the request along the chain until an object handles it. [ Liar design mode ]
Features: a request, along the authority of the low-to-high request, until the right to handle and dispose of it.
tip: The request is passed along the chain until an object has processed it. To ask, in the end did not deal with how to do? This is to avoid, in the design of the need to be thoughtful, and finally the best use of the wrong boot statement.
Use: For a request, assuming the authority to be audited, the ability to process.
Design pattern--Behavioral pattern (i)