Builder Pattern in Dart & Flutter | Why is Builder Pattern not used anymore?
Welcome to the 5th episode of Design Patterns in Flutter Series! In this episode, we will take a look at Builder Pattern.
This series is a YouTube Series — 5 videos on which have already been published. You can check it out here: https://youtube.com/playlist?list=PLlzmAWV2yTgB-1LdoO-9vCTgrE-1dyWKP
What is Builder Pattern (Definition)
Separate the construction of a complex object from its representation so that the same construction process can create different representations.
What does it mean🥲 (Analogy)
Suppose you got an order to make Pizza. So, now you are trying to make Pizza. Pizza toppings cannot be added at any step of the process, right? There’s a step by step process for it. And you cannot follow these steps as it is, for all the orders you might get in the future. Why? Because all orders are different. Some might need topping, extra sauce. Some might not. That’s what Builder Pattern is. Read the definition again. Separate the construction of a complex object from its representation — While creating pizzas, we separate the construction of it by dividing the whole process into various steps (first, take dough, add sauce etc) and the same construction process can create different representations — So with the construction process, we can create different versions of the same pizza (As previously mentioned, we can make slight differences to cater every customer!)
Code Example!
Let’s continue this pizza analogy and implement it in the form of code. Let’s create a Pizza class like this:
Now, we want to create instance of this Pizza class. It will depend on the type of constructor you create. If it’s positional, it will be something like this:
The problem here is that when I don’t want toppings or sauce to be there, I still have to pass a value of that particular data type. For String, I am passing none, For List, I am passing empty list. You might say “Just add a nullable(Ability for a Data Type to have ? like String?) option there”. Even then, we will have to pass null when the user doesn’t want to have toppings or sauce. We want to avoid that. This is what Builder Pattern solves.
So, we will create a Builder class, let’s name it PizzaBuilder and it will be a similar thing to our Pizza class we previously created (name difference, properties are now public and nullable).
And now, we will modify the existing Pizza class to something like this:
Guess the changes? We turned this class immutable! All the properties are now final, meaning they cannot be changed once value is assigned to them. And in the constructor, as you can see, we are taking a PizzaBuilder class and assigning its properties to each variable. Final step is to create this. This is how its done:
What’s the difference?
We need to write more code! That’s the difference. What other? Now, I don’t need to mention fields. It will automatically take the fields not entered as null, no explicit writing! Time saved by writing more code👀
Why It’s Not Used?
Builder Pattern is not used in Dart because there’s an easier way of doing it. We solved two problems right now.
- We made Pizza immutable and there’s a class called PizzaBuilder which we use to change properties of Pizza class outside its file.
- We don’t have to type null, ‘’, false, 0, [] for properties when they are not there.
But we can solve these problems even more simply in Dart. This way:
We have an immutable class. All the properties are final. All we need to do to change its properties outside of this file is use `copyWith` function. And the work is done! No Builder classes. No more complication of managing the same properties between two classes!
So this was all about Builder Pattern and why it’s not used in Dart anymore. If you’re interested in episode 6 of this series, check out my YouTube Channel — Rivaan Ranawat.
Thanks for reading this article, see you in the YouTube videos.