Hello Delegates, Goodby Base Classes

Mohammad AlBdour
3 min readOct 11, 2022

--

Build base-free Android apps by using reusable delegates.

Photo by Danial Igdery on Unsplash

One of the things we used to do when starting new Android projects is set its Base classes, like BaseActivity, BaseFragment, BaseViewModel, BaseRecyclerAdapter…. and so on. So we have a structure to follow and not repeat our code over and over again.

But wait a minute, is that following the SOLID principles? What about the Single Responsibility principle? What about big God basses? What about all child classes has a functionality that they may not use at all? What about making a change in that Base class? Is that going to break all classes that inherit from it? What if we need different variants of that Base class?

We can see that the benefit of bases comes with many problems and high risks. Any change of that base requires changing a lot of classes, a lot of time, a lot of effort, and a lot of testing…. because things are tightly coupled.

In general, to reuse our code, we have multiple relationship options :

1- Inheritance

It derives one class from another, so the relationship is: “is-a”, Like Cat is an Animal. It should be avoided in general in favor of association if we can. Also, in OOP, we can’t inherit from multiple classes.

For more info on this, see the Diamond Problem.

2- Association

It defines a class as the sum of its parts (other classes). So whenever we need some functionality, we can call other objects and use their functionality in our class. Here the relationship is: “has-a”, Like Car has-an Engine.

Association can be achieved by :

1- Aggregation

It is a weak association like every employee has an access card. Employees can exist on their own, and so the access card is.

2- Composition

It is a strong association, like a house has-a room, but a room can’t exist without a house. So the lifecycle of a room is controlled by the house.

After all this, can we use association just like inheritance? Yes, Here is where Delegates shine!

Let’s start with the fun part. I will demonstrate how delegates work with adding the functionality of a ToolBar to an Android Activity.

To apply the delegate's concept, we have to follow 4 steps:

Step 1

Define the functionality that this delegate is responsible for.

Step 1

Step: 2

Implement this functionality.

Step: 2

Step: 3

Instantiate any of these delegate implementations.

Step: 4

Make your class implement this delegate interface, then delegate its functionality.

Step: 3 & Step: 4

Bonus:

This is a lot of added code. Can we simplify things?

Sure we can! Luckily Kotlin has out-of-the-box support for delegates! It’s the magic of the keyword “by”.

Photo by Aziz Acharki on Unsplash

Let’s see that in action:

Kotlin out-of-the-box support for delegates

I hope this article will help you have a cleaner code that is easy to maintain.

For more info see the Kotlin official documentation.

Thanks for reading, and stay tuned for more!

--

--

Mohammad AlBdour

Senior Android Engineer | PSD™ & PSM™ | Open Source Contributor | Writer | Find me on https://www.linkedin.com/in/mobidroid92