"Zen design pattern" of the chain of responsibility pattern

Definitions 1. chain of responsibility pattern

A plurality of object a chance to handle the request, thereby avoiding coupling between the sender and receiver of the request. These objects together into a chain, and pass the request along the chain until an object handles it so far.

Key chain of responsibility pattern is in the "chain" by a chain to handle similar requests to determine who processes the request in the chain.

Second, the application chain of responsibility pattern

Advantages of Chain of Responsibility pattern

Chain of Responsibility pattern very significant advantage is requested and processed separately. Requester may not know who processed the request handler can not know the whole picture (eg J2EE project development, you can strip out the stateless Bean handled by the chain of responsibility), both decoupling and improve the flexibility of the system.

2. shortcomings chain of responsibility pattern

Chain of Responsibility has two significant drawbacks:
First, performance problems, traversing each request from the head of the chain to the end of a chain, particularly when relatively long chain, performance is a very big problem.
The second is not very easy to debug, especially the chain longer, more links, because of using a similar recursive manner, when debugging the logic can be complex.

Note 3. chain of responsibility pattern

Need to control the number of nodes in the chain, to avoid occurrence of long-chain, the general practice is to set a maximum number of nodes in the Handler, it is determined whether the process setNext its threshold is exceeded, beyond which the chain is allowed to establish, to avoid unconscious destruction system performance.

Third, the best practice

Chain of Responsibility pattern masked processing requests, you initiate a request in the end who is treated, and that you do not care, as long as you handle the first request to throw the chain of responsibility, and ultimately return a processing result (of course, can without any treatment), as the requester can not know in the end it is who is going to need to deal with, which is the core chain of responsibility pattern, while the chain of responsibility pattern can also be used as a remedy mode.
Here is a simple example:
as when the project development, needs to confirm this: a request, a handler, but as your business grows, the number and type handlers has increased, this time you can be the first behind the handler to establish a chain, the chain of responsibility is to process the request, if the yuan, good, or the first business logic to handle; if the US dollar, good, pass to the second business logic to process; the yen, the euro ... these do not have a significant change in the original business logic, it can be a good time to address these changing requirements by extending the implementation class.

Code examples: https://github.com/developers-youcong/DesignPatternPractice/tree/master/ChainOfResponsibility

Guess you like

Origin www.cnblogs.com/youcong/p/12185093.html