Chain of responsibility

from Wikipedia, the free encyclopedia

The chain of responsibility (English chain of responsibility is) an inserted in software development design patterns . It belongs to the category of behavioral patterns (English behavioral design patterns ) and for algorithms used. It is used to decouple the trigger of a request from its recipient. The pattern is one of the so-called GoF patterns (see Gang of Four , Gang of Four ).

use

Several objects are switched one behind the other (chained together) in order to be able to process an incoming request together. This request is routed along the chain until one of the objects can answer the request. The client from whom the request originates has no knowledge of the object from which the request will be answered.

UML diagram

Chain of responsibility in UML notation

actors

There are three actors involved in a chain of responsibility:

  • Processor who defines an interface for the requests
  • Specific processor who processes all inquiries for which he is responsible and forwards all other inquiries to the next link in the chain (the next processor)
  • Client who initiates the request to any specific processor.

Advantages and disadvantages

One advantage is that the client does not have to know who is actually responsible. Even the chain links only need to know their direct successor and not the overall structure of the chain. This leads to a lower coupling . In addition, the responsibility of objects for certain requests can be changed without the need to notify potential clients.

On the other hand, there is no guarantee that the request will actually be processed. If the last link in the chain receives a request for which it is also not responsible, the request is rejected according to the above pattern. This must be counteracted by appropriate case treatment.

It must be ensured that each processor appears only once in the chain, otherwise circles will arise and the program will get stuck in an endless loop.

Related design patterns

A related design pattern is the decorator : Before or after a request is forwarded, additional operations can be performed, such as validation checks. The compound word is also similar . The request is passed on from the child to the parent until it is answered or no further object follows. Another possibility results from an iterator over objects with template methods .

Individual evidence

  1. Erich Gamma , Richard Helm , Ralph Johnson , John Vlissides : Design pattern . 5th edition. Addison-Wesley , 1996, ISBN 3-8273-1862-9 , pp. 410 .