Chain of Responsibility pattern --- behavioral

 

 

scenes to be used:

 (1) If there are multiple object you can process the same request, but by the specific object to which the request is processed, the operation time is dynamically determined. This situation can use duty chain, to achieve the object to handle requests becomes the responsibility of the object, then they form a chain of responsibility, when a request is transmitted in this chain, which is processed by specific responsibilities object dynamically at runtime judgment.

(2) If you want to without explicitly specify the recipient, submit a request to the plurality of objects which you can use the Chain of Responsibility pattern. Duty chain to achieve a decoupling between the requester and the recipient, the requestor does not need to know which a receiver object to handle the request.

(3) if you want to specify the dynamic object request processing set, duty chain may be used. Duty chain dynamically build duty chain, i.e. in the end which is dynamically determined duty object to be involved in the processing request, corresponding to a dynamically assigned responsibilities object processing a request set.

3 related patterns

(1) expensive chain functional mode and the combination mode

    These two modes can be combined. Responsibilities objects can be combined by combined mode, which automatically calls up recursively by combining the objects, as a successor to the parent component subassembly to form a chain. This is the previously mentioned using existing external links, in this case, when the client uses, will not need to construct a chain, though not constructed chain, but need to construct a composite object tree, is the same.

(2) duty chain and decorative patterns

    These two modes is similar, from a certain angle, can be simulated each other to achieve functions of decorative patterns can be dynamically added to the target functional decoration, decorative objects and the required decorative objects implement the same interface. And post your chain model enables dynamic combination of responsibilities, there is a standard function object processing ends, but if you finished processing their own in no hurry to knot the East, but following the request is passed down the line, then its function and decorative patterns the function of almost every role is similar to decorative objects, you can achieve a certain function. And the essence of the two modes are also similar, and both need to run during dynamic combination, decorative pattern is a dynamic combination decorators, and post your chain is the chain of responsibility objects but dynamic combination of processing requests from the standard design pattern is concerned, these two there are very different patterns, which point to note. First, different purposes, decorative pattern is to add functionality to achieve transparent objects, and post your chain model is to achieve the sender and receiver decoupling; another, decorative pattern is infinite recursive call, you can have any number of objects to decorative features, but your job is to have a chain mode process is terminated.

(3) duty chain and Strategy Mode

    These two modes can be combined. These two models are similar, if the chain to simplify your job directly to the appropriate treatment will be able to choose the object selection policy would be almost like pattern, so you can use the Chain of Responsibility pattern to simulate the function of the policy. If we simplify the chain of responsibility just to this point, the chain does not exist, it can not be called a chain of responsibility. Two modes can be used in combination, can be a time duty duty chain implementations using a policy to select a specific mode of implementation, the same can also be a policy implementation strategy mode, using your duty chain model to achieve functional deal with. Similarly duty chain and status patterns may be used in combination.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

0

Guess you like

Origin www.cnblogs.com/youngao/p/11401296.html