The third major type of design pattern-behavior pattern.Observer mode, template method mode, command mode, status mode, and responsibility chain modeThese five post-read summaries are welcomed!
Observer mode (observer ):Defines a one-to-many dependency between objects. When the status of an object changes, all objects dependent on it are notified and automatically updated. [Big talk design model]
Features:Similar to object-oriented polymorphism, object-oriented polymorphism only describes that the same object shows different States at different times and under different conditions, the polymorphism of the observer mode focuses on the response of different objects caused by an operation (or command) at the same time. The response can be the same and different, different functions and phenomena can be displayed.
Usage:When an object needs to be changed at the same time.
Assistance:Delegate is used to solve the problem of different names in the "Observer Pattern polymorphism" method, and it is impossible to unify the action.
Template Method ):Defines the algorithm skeleton of an operation, and delays some steps to the subclass. The template method allows the subclass to not change the structure of an algorithm or define some specific steps of the algorithm. [Big talk design model]
Features:Reuse a large amount of code, rather than copying it. In this way, the instance consistency under the template is ensured objectively, and convenience is provided in the later maintenance. It has exactly the same meaning as Object-oriented Inheritance.
Usage:When we want to complete a process or a series of steps at a specific level of detail, but the implementation of individual steps at a more detailed level may be different, we usually consider using the template method mode.
Command mode ):Encapsulate a request as an object so that you can parameterize the client using different requests. You can queue requests or record request logs and support unrecoverable operations. [Big talk design model]
Features:You can queue requests and process commands in sequence. You can also revoke the commands.
Tip:The agile development principle tells us not to add functionality that is not actually needed based on guesses for the code.
Usage:If you do not know whether a system requires a command mode, do not rush to practice it. In fact, it is not difficult to implement this mode through refactoring when necessary, it makes sense to refactor the original code to the command mode only when you really need such features as Undo/restore operations. [Big talk design model]
State ):When the internal state of an object changes, it is allowed to change its behavior. This object seems to have changed its class.
Features:The State mode distributes various state transfer logics to sub-classes of the State to reduce their dependencies.
Tip:When using the statements, you can use them to determine the State. These statements divide the state changes into stages. Note that the Division scope should be fully considered, avoid Omission or overlap to avoid errors. If the partition is broken due to the actual situation, it is best to avoid using the wrong Bootstrap statement.
Usage:When an object's behavior depends on its state, and it must change its behavior according to its state at runtime, you can consider using the state mode.
Chain of responsibility ):So that multiple objects have the opportunity to process requests, so as to avoid coupling between request senders and receivers. Connect these objects into a chain and pass the request along the chain until an object processes it. [Big talk design model]
Features:A request goes from low to high until it has the right to process and process it.
Tip:The request is passed along the chain until an object processes it. I have to ask, what should I do if I haven't done it at the end? This should be avoided. It should be considerate during design. In the end, it is best to use the wrong Bootstrap statement.
Usage: For a request, it can be processed only after the Authority is approved.