All posts
Android, Blog, Java

Design Patterns in Android  –  Builder

Paulina Szklarska

Paulina Szklarska

Design patterns are reusable solutions to the most commonly occurring software problems. They can speed up the development process by providing a proven way of resolving frequent issues.
Discover Builder, which is one of the most common design patterns & check out some examples of Android implementations in practice!

Introduction

Design patterns can be divided into three sections:

  • Creational Design Patterns deliver solutions for creating classes and objects (Singleton, Factory, Builder, etc.)
  • Structural Design Patterns relate to the arrangement of classes and objects (e.g. Composite, Facade, Adapter)
  • Behavioral Design Patterns give us ways to communicate between objects and classes (Command, Observer, Strategy, etc.)

Builder

The Builder pattern simplifies object creation in a very clean and readable way. It’s very helpful when we have some model classes with many parameters. We can make some of them optional, or required, and we don’t force the user to use a specific order (as found in the constructor). By using this Builder pattern, the result is an elegant chain of methods.

The most common use can be found in the AlertDialog.Builder() class:

How can we create the Builder class for our own usage?

Builder in practice

Let’s assume we have some model class for the user:

So, instead of creating objects of this class using constructors, we want to create them using a Builder pattern like this:

How can we do this? First of all, we need to create a constructor with all the parameters in model class User

Then, we need to create a static Builder class inside the User class, which will have the methods to build our object. The key to having chaining methods is ensuring that the builder methods return a Builder class. Look at this example:

For every parameter, we have a setter – the difference is that those methods return a Builder type. At the end, we have a method which uses a constructor from the User class and returns a User type – this is the part where our mess is kept hidden.

Of course, we can make some of the parameters required (as for now, all are optional) by modifying our create() method and throwing some exceptions, e.g.:

That’s it. Through this, we have created our User.Builder() class!

Builder – one pro-tip

If you were patient enough to get through the whole blog post, I have one tip for you regarding Builder patterns: you can generate the whole Builder class using IntelliJ!

All you need to do is place the caret on the constructor in your class and choose Refactor-> Replace Constructor with Builder in the context menu. The Builder class, complete with all the methods, will be auto generated and ready for use.

You can read more here: IntelliJ: Replace Constructor with Builder

Conclusion

Builder pattern is a great approach, not only for model classes but for every object which has more than three or four parameters. With a bit of additional work, we can increase the readability of our code. Design patterns are recognized as the best practice, so it’s big advantage if you know some of them and Builder is a good one to start with.

Newsletter

The post is created by Droids On Roids Team Member.
We would love to take care of your business.

Leave comment

  • Great examples, thanks Paulina!
    In case of Android programming and its first-class citizen language – Kotlin it’s good to notice that Builder pattern can be easily replaced by named parameters and default values in methods’ signatures 🙂