What is the difference between purchasing and assembling DIY IKEA furniture vis-a-vis hiring a carpenter to build your furniture for you? In the second case, you are using the builder pattern.
Builder pattern in real life
If you have ever baked your own cake, I urge you to take the time to remember the day you baked for the first time. Did you get a perfect cake or was it a mess? If you are like me, you would have got some concoction which can hardly be called a cake. During our learning phase, the exact order of steps for baking a good cake is lost on us. We forget steps or ingredients. We forget the baking soda or forget to set the right temperature for the type of bakery we are attempting. We may have added to too much sugar or not beaten the mixture well enough.
Until we are competent, we take no chances during special occasions. We go to the neighbourhood baker and ask for the specific cake by name. “I want half a kilo of black forest cake.” or “Can you make me a pineapple pastry?”, thus go our requests. If the baker is really good, we even agree to buy multi-grain bread from him everyday.
Why does the baker get it right every time? The baker is well-versed with the steps and their order to produce any bakery item. That’s his job and he has made at least 1,000 items of bakery more than you have! His daily habits have made him come up with a system of steps, even a checklist, to make sure that he doesn’t goof up a single order. He has trained himself to follow the same steps in the same order for any item of bakery.
Hold on! Am I saying that whether the item is bread, cookie, cake or pastry, the baker follows exactly the same steps in the same order for each item? That’s EXACTLY what I am saying. A good baker hardly wavers in the sequence of steps across bakery items. However, the recipe and the ingredients change every time. Whole wheat in bread makes way for chickpea flour in a protein loaf. If he uses baking powder for a pastry, he uses yeast for bread and baking soda for instant cookie. He may use molten chocolate for dark forest cake, but pineapple pulp for pineapple pastry. Cakes and pastries will see the oven set in convection mode with a baking time of 30 – 45 minutes while being contained in an aluminium baking tray, but instant cookies will be done inside silicone moulds in microwave mode in a minute or less. The procedure remains something like this for ALL the bakery items.
- Mix dry ingredients mentioned in the recipe.
- Mix wet ingredients mentioned in the recipe.
- Combine dry with wet.
- Add rising agent mentioned in recipe.
- Beat well to form smooth batter.
- Set aside for the time mentioned in recipe.
- Bake in a vessel, with cooking mode and at a temperature mentioned in recipe.
The recipes are also systematic checklists, each carefully categorising the dry ingredients, wet ingredients, rising agent, set-aside time, vessel, cooking mode and temperature.
The list of steps and the systematically categorised recipes are what seperates the professional baker from the rest of us.
So, what is builder pattern?
Let’s take the above example of a baker and his recipes to bake a cake. In a chaotic world, we try to bake our own cake and often forget the sequence of steps or certain ingredients. However the builder pattern defines a system with the following roles.
- Target objects: A variety of objects, all made the same way, but each showing enough difference from the others to bring a different experience to us. Using the builder pattern leads to the creation of target objects. In a bakery, the bread, cakes, pastries, biscuits and cookies are the objects. That’s what the customers are in the bakery for.
- Builders: The builder object stores the knowledge required to create a certain type of target object in a carefully categorised form, but it does not dictate the order in which the actions need to be performed. In an ideal system, one builder carries the knowledge for exactly ONE target object. But for flexibility, the same builder can build at most two or three types with minor differences. In the bakery, the recipes are the builders. They carefully categorise the knowledge need to make a certain type of bakery item. Dry ingredients, wet ingredients, the baking time, etc are all carefully recorded for the type of bakery item required. A cookie recipe contains the ingredients and conditions needed for a cookie, while a bread recipe contains those for bread.
- Builder interface: This is a template to be followed by all builders. All builder objects must contain the methods that this interface declares. If you do not understand what that means, I suggest reading Introduction to object-oriented software and about interfaces, classes and objects. Alternatively, an example will make it clear. E.g. a Bakery Recipe is generic temple for all recipes. Each bakery recipe must tell you dry ingredients, wet ingredients, the rising agent, the set-aside time, the oven mode to use, the vessel to use and the temperature at which to bake. Failing to define any of these is an invalid recipe.
- Director: is the object that knows the sequence of steps for the creating a target object. The director uses the knowledge of a builder object, while following the steps in a sequence known to itself. The end result is a target object desired by the client system. In the bakery, the baker is the director. He uses the same steps for all bakery items, but refers to different recipes for ingredients. In the end, with his carefully laid out sequence of steps and meticulously prepared recipes, he gets a delicious item of bakery every time he is done using his system.
Here is a diagram that shows the builder pattern at work.
Builder pattern in software
Drag-and-drop drawing software is an excellent example of using builder pattern. What do you do when you choose a particular shape to draw? You are activating a particular builder. When you choose to draw a rectangle, a rectangle builder is activated by your app. Same goes for circles, triangles, polygons, etc.
The canvas on the screen, which is the director, always performs the same steps. It uses the stroke colour to stroke the shape and the fill colour to fill the shape. However, the knowledge of how to draw the shape is with the builder object. For a circle, it will draw a single curved line that starts and ends at the same point. For a rectangle, it will draw a straight line, change direction by 90 degrees and repeat the action 3 more times until it closes the loop.
As a user, you get a perfectly straight-edged rectangle or a perfectly curvaceous circle drawn in your favourite colours. And all you had to do was drag and drop.
DIY is a good thing while learning, but to do things in a professional way, an expert is needed. If your software library is causing programmers to fumble with the creation of objects, especially with steps in the wrong order, I suggest giving the builder pattern a try.