CP3 - Builder - Design Pattern From Simple Things
Building a house involves bringing together many parts, each house has its own specific requirements, you need a builder who can easily implement what you want.
Builder is a creational design pattern: 🖤🤍🤍🤍🤍
Design patterns can often be replaced by built-in language features so some
pattern/languagedon’t need to be implemented anymore.Builder/Pythonis a good example of that evil!
What is Builder?
Builder is the guy helping you to create object based on your requirements, tell the builder what you want, then he will give the expected outcome
Why use Builder?
I don’t know about other languages, but in Python you don’t need it, It looks stupid when you use Builder with Python.
Question: Why Builder is stupid in Python?
Answer: Since Python supports Named Parameters 🤖 , the built-in feature is nicer than Builder 👷♂️
When to use Builder?
Question: If I still want to use Builder, when can I use it?
Answer: When you need to create multiple variations of a class
Input:
- Knowing how to build house parts:
Ground FloorFirst FloorRoof
# nothing here
Expected Output:
- Getting 3 houses:
- House without
Roof - Simple House (House without
First Floor) - Advanced House (House has all parts:
Ground Floor, First Floor, Roof)
- House without
O
U
--
^
U
--
^
O
U
How to implement Builder?
Non-Builder implementation:
class NormalHouse:
def __init__(self, has_ground_floor=False, has_first_floor=False, has_roof=False):
self.parts = []
if has_ground_floor:
ground = "U"
self.parts.append(ground)
if has_first_floor:
first_floor = "O"
self.parts.append(first_floor)
if has_roof:
roof = "^"
self.parts.append(roof)
def show_off(self):
print("\n".join(self.parts[::-1]))
if __name__ == "__main__":
normal_house = NormalHouse(has_ground_floor=True, has_first_floor=True)
normal_house.show_off()
print("--")
simple_house = NormalHouse(has_ground_floor=True, has_roof=True)
simple_house.show_off()
print("--")
normal_house = NormalHouse(
has_ground_floor=True, has_first_floor=True, has_roof=True
)
normal_house.show_off()
Builder implementation:
from abc import abstractmethod
class Builder:
@property
@abstractmethod
def house(self):
pass
@abstractmethod
def build_roof(self):
pass
@abstractmethod
def build_ground_floor(self):
pass
@abstractmethod
def build_first_floor(self):
pass
class NormalHouseBuilder(Builder):
def __init__(self):
self.reset()
def reset(self):
self._house = NormalHouse()
@property
def house(self):
house = self._house
self.reset()
return house
def build_roof(self):
roof = "^"
self._house.add(roof)
def build_ground_floor(self):
ground = "U"
self._house.add(ground)
def build_first_floor(self):
first_floor = "O"
self._house.add(first_floor)
class NormalHouse:
def __init__(self):
self.parts = []
def add(self, part):
self.parts.append(part)
def show_off(self):
print("\n".join(self.parts[::-1]))
class Director:
def __init__(self, builder):
self.builder = builder
def build_simple_house(self):
self.builder.build_ground_floor()
self.builder.build_roof()
def build_advanced_house(self):
self.builder.build_ground_floor()
self.builder.build_first_floor()
self.builder.build_roof()
if __name__ == "__main__":
builder = NormalHouseBuilder()
builder.build_ground_floor()
builder.build_first_floor()
builder.house.show_off()
print("--")
director = Director(builder)
director.build_simple_house()
builder.house.show_off()
print("--")
director.build_advanced_house()
builder.house.show_off()
Related posts
-
Verify vs Cert: The Python Requests Handbook
Understanding SSL/TLS in Python Requests: The 'verify' and 'cert' arguments explained with interactive animations.
-
SP7 - Proxy - Learn Design Pattern From Simple Things
The boss wants the employees to focus on work and not get distracted by social media. So he decides to block some websites on the corporate network during working hours.
-
SP6 - Object Pool - Learn Design Pattern From Simple Things
Producing planes on a large scale is expensive, but fortunately the manufactured raw parts are always stored in the pool, thereby reducing duplication in the production process.
-
SP5 - Facade - Learn Design Pattern From Simple Things
There are many departments in the building and you feel confused! By opening the entrances from the facade according to purposes, you simply follow the pre-arranged flow.