Professional Documents
Culture Documents
Factory and Abstract Factory in Android
Factory and Abstract Factory in Android
Search
Listen Share
Design Patterns have long been considered some of the most reliable and useful
approaches for solving common software design problems.
Most of the android developers started their development carrier with Android
without having a complete understanding of the software architecture principles
and design patterns. Though in the small applications these things work out but, as
the application grows the bug count increases, the code base becomes messy.
In this series, I will be discussing a number of patterns that can be used in Android
applications.
Today I will be going through the Factory and Abstract Factory patterns. We will
start from a very simple code base and iterate over the implementations to improve
it and make use of these patterns.
Our app allows a user to create their own customized sandwich in which they can
choose the kind of bread or the filling they wish to have.
Ok! Now, this sounds easy right, all we need to do is to create a different class for
every bread type and every filling. We will give options to the user to select among
those and we will be done right.
class Baguette {
fun getName(): String {
return "Baguette";
}
class Brioche {
fun getName(): String {
return "Brioche"
}
class Cheese {
fun getName(): String {
return "Cheese"
}
class Tomato {
fun getName(): String {
return "Tomato"
}
https://hitenpannu.medium.com/factory-and-abstract-factory-in-android-c93bd541b198 2/17
31/03/2024, 11:55 Factory And Abstract Factory in Android | by hitender pannu | Medium
We have our classes for bread and fillings, now we are ready to use them and
nothing will go wrong here if we are careful enough while using these classes.
This looks good. We have created a new object Brioche and Tomato and assigned it
to our bread and filling variables but there is a small problem as we know that
Brioche is bread and Tomato is a filling but our code has no idea.
So what to do now?
Ok, We know how to fix this we need to create a “type” using interface and make our
classes implement that interface.
Let's do that
interface Bread {}
interface Filling {}
}
}
Cool, let's take another look at the code. We have common methods, and we can
include them in our interfaces.
interface Bread {
fun getName(): String;
fun getCaloriesCount(): String;
}
interface Filling {
fun getName():String
fun getCaloriesCount():String
}
https://hitenpannu.medium.com/factory-and-abstract-factory-in-android-c93bd541b198 4/17
31/03/2024, 11:55 Factory And Abstract Factory in Android | by hitender pannu | Medium
This is perfect, now it will be easy to differentiate that bread and fillings.
But our usage code still looks the same. We still need to use the constructor of
Brioche and Tomato. Wouldn’t it be nice if we can have someone to whom we can
say “hey, I want this type of bread” and it will just create it and give it to us saving us
from writing the constructor logic?
This is where the factory pattern comes into the picture. We just tell our factory that
we want this type of object and it will create it and give it to us saving us from the
https://hitenpannu.medium.com/factory-and-abstract-factory-in-android-c93bd541b198 5/17
31/03/2024, 11:55 Factory And Abstract Factory in Android | by hitender pannu | Medium
constructor logic.
One thing to note here is that a factory should return only a single type of object.
class BreadFactory {
fun getBread(breadType: String): Bread? {
return when (breadType) {
"BRI" -> Brioche()
"BAG" -> Baguette()
"ROL" -> Roll()
else -> null
}
}
}
class FillingFactory {
fun getFilling(fillingType: String): Filling? {
return when (fillingType) {
"CHE" -> Cheese()
"TOM" -> Tomato()
else -> null
}
}
}
The additional benefit of this is that by looking at our factory we can know all the
types of bread or types of fillings supported. This also makes it easy to add more
types or breads and fillings.
What we did above is called factory pattern i.e creating a factory that will return us
a particular type of object separating the construction logic from use.
Everything looks good here and our app is also looking great and things are working
perfectly as expected. Now with the growth of the business, We want to introduce
more items in our app e.g drinks, cookies etc.
https://hitenpannu.medium.com/factory-and-abstract-factory-in-android-c93bd541b198 6/17
31/03/2024, 11:55 Factory And Abstract Factory in Android | by hitender pannu | Medium
Let's have another look at our code base and see if we can improve it.
When we look at any factory, we know all the implementations it can provide,
wouldn’t be nice if we can have a factory which can provide us with different
factories. In this way can make a single point of getting all the factories and our
code base will be more organized.
We know that a factory returns the same type of object so let's create that type.
Now we will create a factory which will return other factory let’s call it
FactoryGenerator.
class FactoryGenerator {
companion object {
fun getFactory(factory: String): AbstractFactory? {
return when (factory) {
"BRE" -> BreadFactory();
"FIL" -> FillingFactory();
else -> null
}
}
}
}
what we did now is called Abstract Factory Pattern i.e. A Factory that returns other
factories.
BONUS:
If you like generics we can modify our abstract factory, a little and our code will
change like this
companion object {
fun getBreadFactory(): AbstractFactory<Bread> {
return BreadFactory();
}
You can also create a live template for this. I have created one you can add it by
going to Preferences-> Editor-> Live Templates-> kotlin
https://hitenpannu.medium.com/factory-and-abstract-factory-in-android-c93bd541b198 9/17
31/03/2024, 11:55 Factory And Abstract Factory in Android | by hitender pannu | Medium
Thanks for spending your valuable time by reading this article. I hope it helps you
out in one or another way-
Next, I will also be sharing more about other design patterns and how we can use
them in our apps.
https://hitenpannu.medium.com/factory-and-abstract-factory-in-android-c93bd541b198 10/17
31/03/2024, 11:55 Factory And Abstract Factory in Android | by hitender pannu | Medium
Follow
Engineer @Coupang
https://hitenpannu.medium.com/factory-and-abstract-factory-in-android-c93bd541b198 11/17
31/03/2024, 11:55 Factory And Abstract Factory in Android | by hitender pannu | Medium
231 3
203 2
https://hitenpannu.medium.com/factory-and-abstract-factory-in-android-c93bd541b198 12/17
31/03/2024, 11:55 Factory And Abstract Factory in Android | by hitender pannu | Medium
hitender pannu
https://hitenpannu.medium.com/factory-and-abstract-factory-in-android-c93bd541b198 13/17
31/03/2024, 11:55 Factory And Abstract Factory in Android | by hitender pannu | Medium
Kocovic Zoran
ssvaghasiya
Builder Design Pattern is a creational design pattern that lets you construct complex objects
step by step. The pattern allows you to…
27
Lists
Sahil Gupta
51 1
https://hitenpannu.medium.com/factory-and-abstract-factory-in-android-c93bd541b198 15/17
31/03/2024, 11:55 Factory And Abstract Factory in Android | by hitender pannu | Medium
Mehar Chand
50
Rizwanul Haque
https://hitenpannu.medium.com/factory-and-abstract-factory-in-android-c93bd541b198 16/17
31/03/2024, 11:55 Factory And Abstract Factory in Android | by hitender pannu | Medium
Why Kotlin Flow is a Better Choice than LiveData for Managing UI-Related
Data in Android…
In the realm of Android app development, managing UI-related data is a critical task. LiveData
has long been a trusted companion for this…
387 1
Amandeep Singh
158
https://hitenpannu.medium.com/factory-and-abstract-factory-in-android-c93bd541b198 17/17