Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Example of `builder' design pattern in Python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Example of `builder' design pattern
# Copyright (C) 2011 Radek Pazdera
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <>.
class Director:
""" Controls the construction process.
Director has a builder associated with him. Director then
delegates building of the smaller parts to the builder and
assembles them together.
__builder = None
def setBuilder(self, builder):
self.__builder = builder
# The algorithm for assembling a car
def getCar(self):
car = Car()
# First goes the body
body = self.__builder.getBody()
# Then engine
engine = self.__builder.getEngine()
# And four wheels
i = 0
while i < 4:
wheel = self.__builder.getWheel()
i += 1
return car
# The whole product
class Car:
""" The final product.
A car is assembled by the `Director' class from
parts made by `Builder'. Both these classes have
influence on the resulting object.
def __init__(self):
self.__wheels = list()
self.__engine = None
self.__body = None
def setBody(self, body):
self.__body = body
def attachWheel(self, 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:
""" Creates various parts of a vehicle.
This class is responsible for constructing all
the parts for a vehicle.
def getWheel(self): pass
def getEngine(self): pass
def getBody(self): pass
class JeepBuilder(Builder):
""" Concrete Builder implementation.
This class builds parts for Jeep's SUVs.
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
class NissanBuilder(Builder):
""" Concrete Builder implementation.
This class builds parts for Nissan's family cars.
def getWheel(self):
wheel = Wheel()
wheel.size = 16
return wheel
def getEngine(self):
engine = Engine()
engine.horsepower = 85
return engine
def getBody(self):
body = Body()
body.shape = "hatchback"
return body
# Car parts
class Wheel:
size = None
class Engine:
horsepower = None
class Body:
shape = None
def main():
jeepBuilder = JeepBuilder()
nissanBuilder = NissanBuilder()
director = Director()
# Build Jeep
print "Jeep"
jeep = director.getCar()
print ""
# Build Nissan
print "Nissan"
nissan = director.getCar()
if __name__ == "__main__":
Copy link

harshraj22 commented Sep 16, 2020

Why would python developer need Builder Design Pattern ?

I read that builder pattern is helpful as languages like Java don't have default named arguments feature in constructors. So creating object of some class having lots of parameters (some of which might be optional), creates a problem. But I think this is not a problem in python. I wonder why would any python developer need to use this pattern. Any counter arguments to enlighten me ?

@pazdera ?


Copy link

snesbittsea commented Oct 7, 2020

I've got a class whose initialization is somewhat complex requiring method calls in a specific order and the generation of intermediate objects that are used in those methods. Trying to do all of this within the init method has been a real pain. And adding an initialization method that one must remember to call isn't an answer IMHO. Delegating construction of this object to another class looks to solve that problem as it allows one to transition class initialization through a number of different states easily.


Copy link

harshraj22 commented Oct 7, 2020

Ah ! I see. Thanks 😃


Copy link

sgrimesRepo commented Jul 23, 2021

Can the builder pattern be used for a rest api client? I'm looking for a flexible way to develop a client for multiple api's.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment