Creational Design Pattern – Builder Patterns

Another creations pattern that lets you construct complex objects is the builder pattern, it is the solution for Anti Pattern called Telescoping Constructor. This anti pattern occurs when developer attempts to create a complex object with excessive use of constructors, Builder pattern tries to solve this issue.

Lets have an analogy here,  Our cake business is booming and we have decided to simulate the process. Now we have 4 types of cake, Sugar Free, Gluten Free, Dairy Free and Full Fat. How do I convert this into code? There are 2 ways

  1. Lets create a Cake class then extend it to all other sub-classes. This would actually involve us to create huge constructors, and also most of the parameters would be unused. So this might not be the best solution to solve this problem.
  2. Builder pattern !!!

 

builder

How builder pattern really works is the builder pattern partitions the complex object i.e.. baking cake into 4 roles. The roles are:

  1. Director (In-charge of building the product)
  2. Abstract Builder (Provides all the required interfaces to build object)
  3. Concrete Builder(Implements interfaces)
  4. Product (Object being built)

build2

 

Here is the implementation of Builder Pattern

class Director():

    def __init__(self, builder):
        self._builder = builder

    def bake_a_cake(self):
        self._builder.make_new_cake()
        self._builder.make_dough()
        self._builder.make_icing()
        self._builder.make_decoration()

    def get_cake(self):
        return self._builder.cake


class Builder():
    """Abstract Builder """
    def __init__(self):
        self.cake = None

    def make_new_cake(self):
        self.cake = Cake()


class CreamCakeBulider(Builder):
    """ Concrete Builder provides all elements to bake the cake """
    def make_dough(self):
        self.cake.dough = "Sugar + Eggs + Dairy"

    def make_icing(self):
        self.cake.icing = "Full Fat Chocolate Icing"

    def make_decoration(self):
        self.cake.decoration = "Cherries with white chocolate flower decoration"


class Cake():
    """ Product"""
    def __init__(self):
        self.dough = None
        self.icing = None
        self.decoration = None

    def __str__(self):
        return '{} | {} | {}'.format(self.dough,self.icing,self.decoration)


builder = CreamCakeBulider()
director = Director(builder)
director.bake_a_cake()
cake = director.get_cake()
print(cake)

>>Sugar + Eggs + Dairy | Full Fat Chocolate Icing | Cherries with white chocolate flower decoration




The strategy followed by Builder pattern is Divide and conquer to reduce complexity in the code

 

Hope this was helpful ! stay tuned for more on Design Patterns.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s