DEV Community

Somenath Mukhopadhyay
Somenath Mukhopadhyay

Posted on

Builder Design Pattern implemented using Python...

Today I am presenting the Builder Pattern in Python.

As the name suggests - it is part of the creational pattern.

The idea is that if a class has many member variables - and the final object may not depend on all of them - and the number of parameters that the constructor takes varies - instead of making a lot of overloaded constructors, we use this Builder pattern

The code footprint is heavily reduced - as there is no need to create a matching number of overloaded constructors to match each and every permutation and combination of the member variables passed in the constructors.

In the given example, we have created a common Student class which may represent a 10th std, a 12th std, or an engineering student - maybe in 1st yr or 2nd year, or third year or maybe in the final fourth year.

In the case of a student who is a qualified engineer, we need to supply all the marks…

But in the case of a 2nd-year student - we don’t need to bother about third-year marks or fourth-year marks, the 10th, 12th, and 1st-year marks are sufficient to represent the object of a second-year student vis-a-vis marks.

Here goes the code for the CS students of the UNIVERSE.

```
from abc import  ABC, abstractmethod  

class Student:  
    class Builder:  

        def __init__(self):  
            self.name=""  
            self.address = ""  
            self.tenthmarks = 0  
            self.twelfthmarks = 0  
            self.firstyrmarks = 0  
            self.secondyrmarks = 0  
            self.thirdyrmarks = 0  
            self.fourthyrmarks = 0  

        def setname(self, name):  
            self.name = name  
            return self  
        def setaddress(self, address):  
            self.address = address  
            return self  
        def settenthmarks(self, tenthmarks):  
            self.tenthmarks = tenthmarks  
            return self  
        def settwelfthmarks(self, twelfthmarks):  
            self.twelfthmarks = twelfthmarks  
            return self  
        def setfirstyrmarks(self, firstyrmarks):  
            self.firstyrmarks = firstyrmarks  
            return self  
        def setsecondyrmarks(self, secondyrmarks):  
            self.secondyrmarks = secondyrmarks  
            return self  
        def setthirdyrmarks9(self, thirdyrmarks):  
            self.thirdyrmarks = thirdyrmarks  
            return self  
        def setfourthyrmarks(self, fourthyrmarks):  
            self.fourthyrmarks = fourthyrmarks  
            return self  

        def build(self):  
            return Student(self)  

    def __init__(self, builder):  
        self.name = builder.name  
        self.addrees = builder.address  
        self.tenthmarks = builder.tenthmarks  
        self.twelfthmarks = builder.twelfthmarks  
        self.firstyrmarks = builder.firstyrmarks  
        self.secondyrmarks = builder.secondyrmarks  
        self.thirdyrmarks = builder.thirdyrmarks  
        self.fourthyrmarks = builder.fourthyrmarks  

    def displayData(self):  
        if self.name != "":  
            print("Name : " , self.name)  

        if self.addrees != "":  
            print("Address : ", self.addrees)  

        if self.tenthmarks != 0:  
            print("10th Marks : ", self.tenthmarks)  

        if self.twelfthmarks != 0:  
            print("12th Marks : ", self.twelfthmarks)  

        if self.firstyrmarks != 0:  
            print("1st Yr Marks : ",self.firstyrmarks)  

        if self.secondyrmarks != 0:  
            print("2nd Yr Marks : ", self.secondyrmarks)  

        if self.thirdyrmarks != 0:  
            print ("3rd Yr marks : ", self.thirdyrmarks)  

        if self.fourthyrmarks != 0:  
            print ("4th Yr Marks : ", self.fourthyrmarks)  


# Press the green button in the gutter to run the script.  
if __name__ == '__main__':  
    ram12th = Student.Builder().setname("Ram").setaddress("Kolkata").


settenthmarks(50).build()  

    shyam2ndYr = Student.Builder().setname("Shyam").setaddress("Delhi")


.settenthmarks(70).settwelfthmarks(78).setfirstyrmarks(70).build()



    ram12th.displayData()



    shyam2ndYr.displayData()  
Enter fullscreen mode Exit fullscreen mode

If we run the above program, the output will be as follows:



Name : Ram

Address : Kolkata

10th Marks : 50

Name : Shyam

Address : Delhi

10th Marks : 70

12th Marks : 78

1st Yr Marks : 70

As it is clear for Ram, till 10th marks was important whereas, for Shyam, on the other hand, marks till 1st year are important.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)