Responsibility Chain Model
The organization of a chain is from the most special to the most general, and there is no guarantee that the request has a corresponding response under any circumstances.
Responsibility chainProgramIn, the responsibility chain reduces the coupling between objects, and each object can be operated independently. The responsibility chain can also be used to construct the objects of the main program and the objects containing other object instances.
Applicable scenarios:
1Objects with the same method are suitable for executing program request operations, but the object determines who will complete the operation.CodeMore suitable
2An object may be the most suitable for processing requests, but you do not want to select a specific object through the IF-else or switch statements.
3When executing a program, you need to add a new object to the processing selected necklace.
4When multiple objects can execute a request, you do not want to put the interaction content in the calling program.
Each object in the chain is "autonomous". The last object determines whether to process requests by default or discard requests.
Command mode
The responsibility chain forwards requests along the class chain, while the command mode only forwards requests to a specific object. In command mode, a request for a specific operation is encapsulated into an object and a well-known public interface is provided to the object, so that the client can generate a request without understanding the actual operation, you can also change the operation without affecting the client.
Command mode:
The main disadvantage of the command mode is that it adds a small class that disconnects the program. However, even if a single click event exists, a small private method is usually called to complete specific functions. The final result is that private methods have almost the same length as the code of these small classes. Therefore, there is usually no difference in complexity between building command classes and writing more methods, the main difference is that small classes generated in command mode are easier to understand.
Another major reason for using the command design pattern is that they provide a convenient storage method and can complete the Undo function. Every command object remembers what has just been done, and when there is an undo request, as long as the calculation amount and memory demand are not too much, it can be restored to the previous state.
Interpreter Mode
The interpreter mode usually describes defining a grammar for a language and interpreting the statements in the language.
Interpreter mode:
As long as the interpreter is used in the program, it is a simple way for the program user to enter the command in this language.
When introducing a Language and Its syntax, you also need to perform a wide range of error checks on spelling words or wrong grammar elements, unless there is available template code to implement this check, otherwise, you need to write a large number of programs.
Advantages of the interpreter mode: Once a general analysis, Induction Tool is built, it is quite easy to expand or modify the syntax. After a rule is established, new verbs or variables can be added. As grammar becomes more complex, it becomes increasingly difficult to maintain programs.
The interpreter is not so common in solving general programming problems.