Handle Deep linking with Chain of Responsibility Pattern

Ahmet Keskin
Trendyol Tech
Published in
4 min readFeb 22, 2021

Most of the iOS apps welcome their users with;

  • Universal links
  • Push notifications
  • Custom scheme

And navigate users to related screens become a necessity. Regardless of whether the application that you are working on is small or big, creating a good deep link mechanism will save the future.

I want to open parentheses that especially if you are working in a dynamic sector like e-commerce, you should think about how to welcome users to your application and future requirements. Because business and marketing requirements get bigger day by day. So your deep link infrastructure should be flexible, extendible and maintainable.

So as you expected, we’ll focus on Chain of responsibility pattern but first i want to share the wikipedia explanation

In object-oriented design, the chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects. Each processing object contains logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. A mechanism also exists for adding new processing objects to the end of this chain.

Enough talk, show me some code ;)

Let’s dive in;

When we create our infra, we have to strict to solid principles. Chain pattern forces us to do it. Let’s start with 👇🏻

  • Single Responsibility → in wikipedia explanation

The single-responsibility principle (SRP) is a computer-programming principle that states that every class in a computer program should have responsibility over a single part of that program’s functionality, which it should encapsulate. All of that module, class or function’s services should be narrowly aligned with that responsibility.

Firstly our deep link module should have one job so we will create a class named DeeplinkManager ( naming is up to you ). With this, we apply the Single Responsibility Principle.

This class is responsible for resolving which deeplink item is satisfied and delegate the real job to satisfied deeplink item

  • Open-Closed Principle → in wikipedia explanation

In object-oriented programming, the open–closed principle states “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification”; that is, such an entity can allow its behaviour to be extended without modifying its source code.

Your system should be open for extension but closed for modification. We need to apply this principle because after we create our deeplink module, we will not change the main mechanism. We’ll continue to extend it according to what we need. Then how do we manage that for deeplink?

Firstly we need to separate every screen that we’ll navigate with deeplink. For that, we’ll create deeplink items for every screen which conforms to the same protocol.

DeeplinkableItem should be conformed by every deeplink item. And its methods responsible that;

  • isSatisfiedBy method should check the condition that you want
  • execute method should navigate the screen which is satisfied by deeplinkManager

Let’s continue with real examples. Assume that you will have two pages which are ProductDetail and OrderDetail to navigate with deeplink. What do you need for opening these screens?

productId for ProductDetail

orderId for OrderDetail

right? Let’s create their deeplink items 👊🏻

We have created our deeplink items. Creating deeplink item for each page with conforming to the same protocol, we have applied one of the behavioral design patterns which is called Strategy Pattern.

  • Strategy Pattern → in wikipedia explanation;

In computer programming, the strategy pattern is a behavioral software design pattern that enables selecting an algorithm at runtime. Instead of implementing a single algorithm directly, code receives run-time instructions as to which in a family of algorithms to use.

With applying Strategy pattern, SOLID’s two main principles are also applied which are open-closed & single responsibility principle.

  • Single Responsibility → every item has one job that satisfied its condition and navigate with its params.
  • Open-Closed → when we need to add a new page which will be needed to open with deeplink, just create an another struct. That’ll not change the main deeplink mechanism

And how do we use these deeplink items? Let’s also dig in for it.

After creating our deeplink items, we are ready to use them. The main idea is here that initialize items and check every item when navigate method is called. If one of them is satisfied, thats the one which should be executed.

Well whats the benefit of this? We could easily implement this;

Just think about when you have 30+ screen to navigate with deeplink -.-

Whatever you used, If conditions or switch case;

  1. It will be ugly that statements goes through to endless
  2. You’ll have to touch navigate method every time when you add a new screen. And that will effect the other screens because you’ll edit the main mechanism.
  3. It’ll not be scalable

Well, i have tried to explain how you can build a structure for deep linking with using chain of responsibility pattern. It’s really powerful pattern and you can apply this approach to anything, not only deep linking 😉

if you want to check, i leave the github link 👇🏻

https://github.com/AhmettKeskin/DeeplinkingWithChainPattern

Thanks for reading 📖 🧑🏻‍💻

--

--