Builder Design Pattern in Android

Kayvan Kaseb
Software Development
4 min readMar 11, 2020
Builder Design Pattern in Android

Nowadays, Design Patterns play essential roles in designing and developing Android apps because they can provide some best practices for having high quality apps and following clean code principles. One of the main design patterns that are used in android development is Builder Design Pattern. In fact, this design pattern could be effective in improving flexibility and readability of codes. This article aims to discuss Builder Design Pattern in Android applications with using Kotlin.

What is the definition of Design Pattern?

Design patterns are used to represent some of the best practices adapted by experienced object-oriented software developers. In software development, a design pattern is a general reusable solution to a commonly occurring problem or task in software design. In other words, it describes the problem, the solution, when to apply the solution, and its consequences. Besides, It gives implementation hints and examples.

Benefits of using Design Patterns in Android

  1. Design Patterns use object-oriented concepts such as decomposition, inheritance, and polymorphism.
  2. They can improve the re-usability and extensibility of Android apps. It means you do not need to write the same code again at various places, and also you can be able to add other features to your app much more easier than general coding. Therefore, Design Patterns can increase speed in application development in Android.
  3. They can make your codes cleaner by decoupling the code. In addition, Design Patterns can enhance the understanding of codes in Android apps.
  4. Design Patterns are proved and testified solutions because they have been built upon the knowledge and experience of expert application developers.
  5. They make communication between developers more efficiently.

Types of Design Patterns

Fundamentally, there are a number of Design Patterns, which can be used in software development, and all these could be categorized into the following three categories:

  1. Creational Design Pattern

They are concerned with the way of creating objects such as Builder, Singleton, and Dependency Injection.

2. Structural Design Pattern

They are discussed how classes and objects can be composed, to form larger structures such as Adapter, Facade, and Proxy.

3. Behavioral Design Pattern

They are considered the interaction and responsibility of objects such as Observer, Mediator, and Interpreter.

Builder Design Pattern

Basically, the Creational Design Pattern is used to create some objects without representing the logic or the steps, which are involved in creating the objects. In fact, hard-Coded code is not an appropriate programming approach. In this category, we are creating the instance by using the new keyword. Occasionally, the nature of the object must be changed according to the nature of the program. In these situations, we must use the Creational Design Patterns to provide more flexibility. Thus, this makes the creation of objects easier.

As a matter of fact, Builder Design Pattern indicates that you should construct a complex object from simple objects by using step-by-step approach. It is mainly used when object cannot be implemented in single step like in the deserialization of a complicated object.

Advantages of using Builder Design Pattern

  1. when you initialize your object, you do not need to have all the data to pass it to your object. As s result, it could be helpful to control over construction process.
  2. It implements a clear separation between the construction and representation of an object. Furthermore, it hides internal representation of the objects from the client.
  3. It handles the changes in the internal representation of objects.
  4. It can improve the flexibility and readability of source codes.
  5. It avoids the Telescoping Constructor Pattern. For instance:
Pizza(int size){
}
Pizza(int size, boolean cheese){
}
Pizza(int size, boolean cheese, boolean meat){
}
Pizza(int size, boolean cheese, boolean meat, boolean bacon){
}

The problem with this pattern is that when a constructor have 4 or 5 parameters long, it becomes difficult to remember those parameters.

A simple example in Kotlin

In Android, the Builder pattern appears when using objects like AlertDialog.Builder. For example:

val builder = AlertDialog.Builder(this)
builder.setTitle("Sample Alert")
builder.setMessage("Sample Message!")


builder.setPositiveButton(android.R.string.yes){ dialog, which ->

}
builder.setNegativeButton(android.R.string.no){ dialog, which ->

}

builder.setNeutralButton("Maybe"){ dialog, which ->

}
builder.show()

This builder proceeds step-by-step, and helps you specify just only the parts of your AlertDialog that you want.

Another Example in Kotlin

In the below example, we have PersonalComputer class with some properties such as cpu, ram, batteryCapacity, and screenSize. In this class, the cpu is significant and compulsory. Therefore, you have to pass it every time, but the rest of the properties are optional. Additionally, if you do not set any value, the default value will apply.

class PersonalComputer(builder: Builder){    private val cpu: String = builder.cpu
private val ram: String = builder.ram
private val batteryCapacity: String = builder.batteryCapacity
private val screenSize: String = builder.screenSize

class Builder(cpu: String){
var cpu: String = cpu
var ram: String = "8G"
var batteryCapacity: String = "10000mAH"
var screenSize: String = "17inch"
fun setRam(ram: String): Builder{ this.ram = ram
return this
}fun setBattery(batteryCapacity: String): Builder{ this.batteryCapacity = batteryCapacity
return this
}fun setScreenSize(screenSize: String): Builder{ this.screenSize = screenSize
return this
}fun build(): PersonalComputer{ return PersonalComputer(this) }
}
}

Now, we can use the Builder class as follows:

PersonalComputer.Builder("i5") 
.setRam("16G")
.setScreenSize("14inch")
.build()

In conclusion, Design patterns play key roles in having a high quality Android apps and following clean code principles. In this essay, using Builder Design Pattern was considered in Android Development. Also, some advantages were explained, such as providing flexibility and readability of codes. However, the number of lines of code increase at least to double in builder pattern.

--

--

Kayvan Kaseb
Software Development

Senior Android Developer, Technical Writer, Researcher, Artist, Founder of PURE SOFTWARE YAZILIM LİMİTED ŞİRKETİ https://www.linkedin.com/in/kayvan-kaseb