Member-only story
The Chain of Responsibility Pattern in Swift
Pass a request along until someone handles it!
Difficulty: Beginner | Easy | Normal | Challenging
This article has been developed using Xcode 11.5 and Swift 5.2.4
The chain of responsibility is an OO version of if … else
in a way that promotes the idea of loose coupling within your App.
Prerequisites:
- You will be expected to be aware of how to make a Single View Application in Swift.
Terminology:
Design Pattern: a general, reusable solution to a commonly occurring problem
Comparison with the decorator design pattern
The chain of responsibility pattern is very similar to the decorator pattern, but only one of the objects in the chain of responsibility.
First responder
Effectively you give multiple objects the opportunity to process a request. One great example of the Chain of Responsibility is the concept of responders.
UIKit
starts with the first responder control that activated the keyboard, and then passes the call up the chain until a component can deal with the action.