Junior — Middle
What implementations of the Builder pattern are applicable in Android development?
sobes.tech AI
Answer from AI
In Android development, the Builder pattern is widely used to simplify the creation of complex objects with many parameters. Main examples:
- AlertDialog.Builder — for step-by-step construction of dialogs with configuration of title, text, buttons, etc.
- NotificationCompat.Builder — for creating notifications with various parameters.
- OkHttpClient.Builder — for configuring an HTTP client.
The pattern allows creating an object gradually by calling methods to set parameters, and finally a build() method that returns the ready object.
Example of using AlertDialog.Builder:
val dialog = AlertDialog.Builder(context)
.setTitle("Attention")
.setMessage("Are you sure?")
.setPositiveButton("Yes") { _, _ -> /* action */ }
.setNegativeButton("No", null)
.create()
dialog.show()