Problem without using the Builder Method
Imagine you want to join one of the elite batches of w3wiki. So, you will go there and ask about the Fee structure, timings available, and batches about the course you want to join. After looking at the system, they will tell you about the courses, their Fee structures, timings available and batches. Thatâs it! (No! we are not done yet because we are good developers).
Our main purpose is to design the system flexible, reliable, organized and lubricative. what Unexperienced developers will do is that they will create a separate and unique class for each and every course provided by w3wiki. Then they will create separate object instantiation for each and every class although which is not required every time. The main problem will arise when w3wiki will start new courses and developers have to add new classes as well because their code is not much flexible.
Note: Following code is written without using the Builder Method.
# concrete course
class DSA():
"""Class for Data Structures and Algorithms"""
def Fee(self):
self.fee = 8000
def available_batches(self):
self.batches = 5
def __str__(self):
return "DSA"
# concrete course
class SDE():
"""Class for Software development Engineer"""
def Fee(self):
self.fee = 10000
def available_batches(self):
self.batches = 4
def __str__(self):
return "SDE"
# concrete course
class STL():
"""class for Standard Template Library of C++"""
def Fee(self):
self.fee = 5000
def available_batches(self):
self.batches = 7
def __str__(self):
return "STL"
# main method
if __name__ == "__main__":
sde = SDE() # object for SDE
dsa = DSA() # object for DSA
stl = STL() # object for STL
print(f'Name of Course: {sde} and its Fee: {sde.fee}')
print(f'Name of Course: {stl} and its Fee: {stl.fee}')
print(f'Name of Course: {dsa} and its Fee: {dsa.fee}')
Builder Method â Python Design Patterns
Builder Method is a Creation Design Pattern which aims to âSeparate the construction of a complex object from its representation so that the same construction process can create different representations.â It allows you to construct complex objects step by step. Here using the same construction code, we can produce different types and representations of the object easily.
It is basically designed to provide flexibility to the solutions to various object creation problems in object-oriented programming.
UML Diagram of Builder Design Pattern
Contact Us