Responsibility chain mode, state mode

Chain of Responsibility (Chain of Responsibility) model

Definition: In order to prevent the request sender from being coupled with multiple request handlers, all request handlers will remember the reference of the next object through the previous object to form a chain; when a request occurs, the request can be connected Along this transmission chain, until there is an object to handle it.

Pros and cons

advantage:

  1. Reduce the degree of coupling between objects . This mode eliminates the need for an object to know which object handles its request and the structure of the chain, and the sender and receiver do not need to have clear messages from each other.
  2. Enhance the scalability of the system . New request processing classes can be added as needed to meet the opening and closing principles;
  3. Enhanced flexibility in assigning responsibilities to objects . When the workflow changes, members in the chain can be dynamically changed or their order can be mobilized, and responsibilities can also be dynamically added or deleted.
  4. The chain of responsibility simplifies the connection between objects . Each object only needs to maintain a reference to its successor, and does not need to maintain references to all other processors, which avoids the use of numerous if or if-else statements
  5. Responsibility sharing . Each class only needs to work hard to handle the work that should not be handled, and pass it to the next object for completion. The responsibility position of each class is clarified, which conforms to the single responsibility principle of the class.

Disadvantages:

  1. There is no guarantee that every request will be processed . Since a request has no clear recipient, there is no guarantee that it will be processed, and the request may not be processed until it reaches the end of the chain.
  2. For a relatively long chain of responsibility, the processing of requests may be designed to multiple processing objects, and system performance will be affected to a certain extent .
  3. The rationality of the establishment of the responsibility chain should be guaranteed by the client, which increases the complexity of the client and may cause system errors due to the incorrect setting of the responsibility chain, for example, it may cause cyclic calls.

Application scenarios

  1. There are multiple objects that can handle a request , and which object handles the request is automatically determined at runtime.
  2. You can dynamically specify a group of objects to process the request, or add a new handler .
  3. If the request processor is not clearly specified, submit a request to one of multiple processors .

main character

  1. Abstract Handler (Handler) role: Define an interface for processing requests, including abstract processing methods and a subsequent connection.
  2. The role of the concrete handler (ConcreteHandler): to implement the processing method of the abstract handler, to determine whether the request can be processed, and if it can handle the request, it will be processed, otherwise the request will be forwarded to his successor.
  3. Client role: Create a processing chain, and submit a request to the specific handler object at the head of the chain. It does not care about the details of the processing and the delivery process of the request.

Structure chart

Insert picture description here

Applications

Use the chain of responsibility to design a leave request approval module.
If it is stipulated that the student's leave is less than or equal to 2 days, the head teacher can approve; less than or equal to 7 days, the dean can approve; less than or equal to 10 days, the dean can approve; otherwise, leave is not granted . The structure diagram is as follows:
Insert picture description here

Extension of the Chain of Responsibility Model

  1. Pure responsibility chain model: a request must be accepted by a certain processing object , and a specific processor can only use one of the following two behaviors to process a certain request:
    a. Deal with it yourself (take responsibility)
    b. Take responsibility Push to the next family for processing
    2. Impure responsibility chain model: Allows a specific processing object to take part of the responsibility of the request and then pass the remaining responsibility to the next family, and a request can ultimately not be accepted by any Received by the end object .

State mode

Definition: For stateful objects, the complex "judgment logic" is extracted into different state objects, allowing the state object to change its behavior when its internal state changes.

Pros and cons

advantage:

  1. The state mode localizes the behaviors related to a specific state into one state, and separates the behaviors of different states to meet the "single responsibility principle" ;
  2. Reduce the interdependence between objects . Introducing different states into independent objects will make state transitions more clear and reduce the interdependence between objects;
  3. Conducive to the expansion of the program . It is easy to add new states and transitions by defining new subclasses.

Disadvantages:

  1. The use of state mode will inevitably increase the number of classes and objects in the system ;
  2. The structure and implementation of the state mode are relatively complicated , and if used improperly, it will cause confusion in the program structure and code.

Application scenarios

  1. When an object's behavior depends on its state , and it must change its behavior according to the state at runtime , you can consider using the state mode.
  2. An operation contains a huge branch structure , and these branches are determined by the state of the object.

main character

  1. Context role: also known as context, it defines the interface that customers are interested in, maintains a current state, and delegates state-related operations to the current state object for processing.
  2. Abstract state (State) class: defines an interface to encapsulate the behavior corresponding to a specific state in the environment object.
  3. Concrete state (ConcreteState) role: realize the behavior corresponding to the abstract state.

Structure chart

Insert picture description here

Applications

This case includes three statuses of "failed", "medium" and "excellent". When a student's score is less than 60 points, it is considered "failed"; when the score is greater than or equal to 60 points and less than 90, it is considered to be "medium"; when the score is greater than or equal to 90 points, it is considered to be "excellent". The structure diagram is as follows:
Insert picture description here

Note: The contents of the article picking in the "software design patterns (Java version)", Author: Cheng fine column

Guess you like

Origin blog.csdn.net/weixin_44048668/article/details/110719879