Design Patterns Builder in Python

Shashank Shanu

17 hours ago

Table of Content
  • Some of the Advantages of Builder Pattern
  • Implementation of a builder pattern?
  • Output
  • Some of the disadvantages of using Builder pattern:
     Python provides a unique pattern Builder which helps us in building a complex object using simple objects and this pattern uses an algorithmic approach. This design pattern falls under the category of a creational pattern.
In Builder patterns, a builder class helps us to build the final object in the step-by-step process which is independent of the other objects.
Design Patterns Builder in Python | Insideaiml
Design Patterns Builder in Python | Insideaiml

Some of the Advantages of Builder Pattern

  • Builder the pattern provides a clear separation and unique layer between construction and representation of a specified object by class.
  • Builder the pattern provides the perfect scenario to change the internal representation of objects.
  • Builder the pattern gives better control over the construction process of the pattern created.

Implementation of a builder pattern?

Let’s take an example and try to understand how the builder pattern is implemented. Example
class Director:
    __builder = none
    
    def setBuilder(self, builder):
        self.__builder = builder
   
    def getCar(self):
        car = Car()
      
      # First goes the body
        body = self.__builder.getBody()
        car.setBody(body)
      
      # Then engine
        engine = self.__builder.getEngine()
        car.setEngine(engine)
      
      # And four wheels
        i = 0
        while i < 4:
            wheel = self.__builder.getWheel()
            car.attachWheel(wheel)
            i += 1
        return car

# The whole product
class Car:
    def __init__(self):
        self.__wheels = list()
        self.__engine = none
        self.__body = none

    def setBody(self, body):
        self.__body = body

    def attachWheel(self, wheel):
        self.__wheels.append(wheel)

    def setEngine(self, engine):
        self.__engine = engine

    def specification(self):
        print("body: %s" % self.__body.shape)
        print("engine horsepower: %d" % self.__engine.horsepower)
        print("tire size: %d\'" % self.__wheels[0].size)

class Builder:
    def getWheel(self): pass
    def getEngine(self): pass
    def getBody(self): pass

class JeepBuilder(Builder):
    def getWheel(self):
        wheel = Wheel()
        wheel.size = 22
        return wheel

    def getEngine(self):
        engine = Engine()
        engine.horsepower = 400
        return engine

    def getBody(self):
        body = Body()
        body.shape = "SUV"
        
        return body

# Car parts
class Wheel:
    size = none

class Engine:
    horsepower = none

class Body:
    shape = none

def main():
    jeepBuilder = JeepBuilder() # initialize the class
   
    director = Director()
   
   # Build Jeep
    print("Jeep")
    director.setBuilder(jeepBuilder)
    jeep = director.getCar()
    jeep.specification()
    print("")

if __name__ == "__main__":
    main()

Output

The result produced by the above code is as follows:
Jeep
body: SUV
engine horsepower: 400
tire size: 22'

Some of the disadvantages of using Builder pattern:

Below are some of the disadvantages of using the builder pattern
  • Code complexity increases as the builder pattern requires multiple new classes.
  • The builder class should be mutable.
  • Class data members are not guaranteed to be initialized.
I hope you enjoyed reading this article and finally, you came to know about Design Patterns Builder in Python.
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…

Submit Review