1 of 19

{Google Developer Group Sonargaon}

2023

Builder Design Pattern

Android Development Perspective

2 of 19

Who I am?

Hasan Abdullah

Technical Lead

Brain Station 23 Ltd

https://hellohasan.com

Currently Contributing in

Muslims Day

Few Previous Client Projects

3 of 19

Let’s start!

Think about a complex object creation.

4 of 19

Example of a constructor with long parameters

5 of 19

Too many parameters

Understanding the problems

Immutability

Telescopic constructors

Order of initialization

6 of 19

Problem: Telescopic Constructor

7 of 19

Multiple network configurations like time out, cache size, retry mechanism etc should be immutable

Where the size and margin of the canvas should be unchangeable. Other painting properties should not be changed again and again

Network Config

Painting on Canvas

notification title, subtitle, icon etc should not be changed after object creation. These properties should be fixed

Notification config

Problem: Immutability

Examples

8 of 19

From animation execution, the order of execution is important.

First of all we need to set the size of the canvas. Background color, margin of the canvas. Then the other objects will be painted.

Animation properties

Painting on canvas

We need to set the title section first. Then the rest of content

Document Builder

Problem: Order of initialization

Examples

9 of 19

What is the solution?

10 of 19

Solution is

Builder Design Pattern

11 of 19

What should we support?

  1. Complex and large number of parameter
  2. Optional parameter
  3. Immutable parameters
  4. Order of initialization
  5. User should not memorise the order

12 of 19

User’s expectations

13 of 19

Builder Pattern Implementation

14 of 19

Few common uses of

Builder Pattern in Android

15 of 19

Ex: Glide Image Loader Library

16 of 19

Ex: Notification Builder

17 of 19

Ex: Retrofit Builder

18 of 19

19 of 19

Thank You

Hasan Abdullah

Technical Lead, Brain Station 23 Ltd