-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethod_template_pattern.py
More file actions
64 lines (45 loc) · 1.44 KB
/
Copy pathmethod_template_pattern.py
File metadata and controls
64 lines (45 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# AbstractClass
class HouseTemplate:
def build_house(self):
self.build_foundation()
self.build_walls()
self.build_roof()
self.decorate()
def build_foundation(self):
raise NotImplementedError(
"build_foundation must be implemented by subclasses")
def build_walls(self):
raise NotImplementedError(
"build_walls must be implemented by subclasses")
def build_roof(self):
raise NotImplementedError(
"build_roof must be implemented by subclasses")
def decorate(self):
# Optional hook method for additional customization
pass
# ConcreteClass
class WoodenHouse(HouseTemplate):
def build_foundation(self):
print("Building Wooden Foundation")
def build_walls(self):
print("Building Wooden Walls")
def build_roof(self):
print("Building Wooden Roof")
# ConcreteClass
class BrickHouse(HouseTemplate):
def build_foundation(self):
print("Building Brick Foundation")
def build_walls(self):
print("Building Brick Walls")
def build_roof(self):
print("Building Brick Roof")
def decorate(self):
print("Adding Brick Decorations")
# Client code
if __name__ == "__main__":
wooden_house = WoodenHouse()
brick_house = BrickHouse()
print("Building Wooden House:")
wooden_house.build_house()
print("\nBuilding Brick House:")
brick_house.build_house()