Design Pattern: Chain of Responsibility Pattern

Tram Ho

I. Introduction:


Chain of Responsibility is a design pattern of the Behavior type. It allows us to handle the event with one or more Handlers. The Chain of Responsibility pattern consists of the main components:

  • Client : This is where the event will be sent to the Handler Protocol . Events can be a struct , class , or data-carrying properties …
  • Handler Protocol : A protocol or Abstract class used to declare properties and methods that Concrete Handlers must have for event handling. ** Abstract Class **, Base Class or protocol are often used to declare Handler Protocol.
  • Concrete Handler : Instances used to inherit or implement the properties and methods declared in the previous Handler Protocol . Here, the method will write detailed logic code to receive and handle the events sent by the client . If in case the Concrete Handler cannot handle the event sent, it will move to the next Concrete Handler to handle.

II. How it works:


The Chain of Responsibility pattern works by converting event listeners into independent objects called Handlers . Handlers will be linked together into a series of consecutive Handlers . When a request (or event) is sent, that request will be forwarded continuously in the Handler chain until it meets a Handler that can process that request . Each Handler has the right to process the request or pass it on to the next Handler in the chain. Each Handler reserves the right to decide whether it will process the Request or forward that Request to the next Handler.

III. When is the Chain of Responsibility used?


  • When you want to separate sender and recipient request.
  • When there are multiple ways to handle the same request is sent to.
  • When you don’t want to explicitly specify how to handle an event sent to.
  • When you want to make a request to one of the objects without specifying which object will receive and process the request.

IV. Chain of Responsibility in iOS:


Chain of Responsibility is used and applied in the iOS UIKit framework . In UIKit , the UIResponder acts as a Handler created and connected in a series of UIResponder together. When receiving an interactive event from UIView , the UIView UIResponder of that UIView will decide to handle the event or continue to forward that event to the Super View of that UIView to receive and handle the event.

In addition, UIKit also uses the Chain of Responsibility pattern in handling its errors . When an Error is sent, the Concrete Handlers will either enter and decide to handle it or move it to the next Concrete Handler to continue the error handling process.

V. For example:


1. Handler Interface:

  • Used to declare the structure of a Handler. The normal Handler Interface will declare a method to handle the Request or sometimes there will be a method to assign a value to the next Handler.

2. Base Handler:

  • A class created to declare data fields that point to the next Handler in the string and implement Handler’s default handling operations. The Client side can create a Handler string using the Contructor or the Setter functions written in this class.

3. Concrete Handler:

  • Handlers created from the inheritance of Base Handlers. Concrete Handlers will implement each action individually. Each Handler will handle the Request in a different way. The Concrete Handle will decide whether to handle the Request itself or pass that Request to the next Concrete Handle of the processing chain.

4. Client:

  • The client is the place where the Handler string is declared and the place to send the request. Requests can be sent to any element in the chain, not required to be the first element.

BECAUSE. References:


  • Design Pattern by Tutorials – Raywenderlich
  • Chain of Responsibility by refactoring.guru
Share the news now

Source : Viblo