Builder Pattern in Android
Builder Pattern in Android
The Builder Pattern is a widely used creational design pattern that constructs complex objects step by step. Instead of relying on multiple constructors with varying parameter lists, a builder object accumulates configuration through method calls and produces the final object in a single build() invocation. This pattern appears throughout the Android SDK and is equally useful in application code when an object requires many optional properties. By the end of this lesson, you will be able to:
- Explain how the Builder Pattern separates construction from representation.
- Identify common Android SDK classes that use the Builder Pattern.
- Implement a custom builder in Kotlin using both the traditional approach and a DSL.
- Describe how Kotlin language features reduce the need for explicit builders.
- Evaluate when the builder pattern is preferable to named arguments with defaults.
- Apply validation in a builder's
build()method to enforce construction invariants.
How the Builder Pattern Works
The pattern relies on two roles. A Builder class exposes setter methods that configure individual properties and returns itself from each call so that methods can be chained. A build() method finalizes construction and returns the target object. The target class typically has a private constructor so that the builder is the only way to create instances.
The Builder Pattern belongs to the family of creational patterns defined in the Gang of Four catalog. Unlike the Factory Method pattern, which hides the concrete type being created, the Builder Pattern focuses on simplifying the construction process itself when the object has many optional fields or requires multi step initialization.
This separation matters when a class has several optional parameters. Without a builder, you would need telescoping constructors (where each constructor adds one more parameter) or a single constructor with many nullable defaults. Telescoping constructors are hard to read because the caller passes positional arguments without labels. A single constructor with defaults works in Kotlin thanks to named arguments, but the builder pattern adds an additional benefit: it can defer validation to the build() call, ensuring the object is fully configured before it is created. The builder makes intent explicit at the call site and prevents accidental misconfiguration.
Builder Pattern in the Android SDK
Android uses the builder approach in several core APIs. AlertDialog.Builder configures title, message, buttons, and callbacks before calling create():
val dialog = AlertDialog.Builder(context)
.setTitle("Confirmation")
.setMessage("Are you sure?")
.setPositiveButton("Yes") { d, _ -> d.dismiss() }
.setNegativeButton("No") { d, _ -> d.dismiss() }
.create()
dialog.show()
NotificationCompat.Builder follows the same pattern for notifications:
This interview continues for subscribers
Subscribe to Dove Letter for full access to exclusive interviews about Android and Kotlin development.
Become a Sponsor