-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathbuilder.py
134 lines (91 loc) · 3.68 KB
/
builder.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Code Listing #8
"""
Builder design pattern implemented as a house builder class example with
a few sub-classes demonstrating the power of the builder pattern.
"""
class Room(object):
""" A class representing a Room in a house """
def __init__(self, nwindows=2, direction='S'):
self.nwindows = nwindows
self.direction = direction
def __str__(self):
return "Room <facing:%s, windows=#%d>" % (self.direction,
self.nwindows)
class Porch(object):
""" A class representing a Porch in a house """
def __init__(self, ndoors=2, direction='W'):
self.ndoors = ndoors
self.direction = direction
def __str__(self):
return "Porch <facing:%s, doors=#%d>" % (self.direction,
self.ndoors)
class LegoHouse(object):
""" A lego house class """
def __init__(self, nrooms=0, nwindows=0,nporches=0):
# windows per room
self.nwindows = nwindows
self.nporches = nporches
self.nrooms = nrooms
self.rooms = []
self.porches = []
def __str__(self):
msg="LegoHouse<rooms=#%d, porches=#%d>\n" % (self.nrooms,
self.nporches)
for i in self.rooms:
msg += str(i) + '\n'
for i in self.porches:
msg += str(i) + '\n'
return msg
def add_room(self,room):
""" Add a room to the house """
self.rooms.append(room)
def add_porch(self,porch):
""" Add a porch to the house """
self.porches.append(porch)
class LegoHouseBuilder(object):
""" Lego house builder class """
def __init__(self, *args, **kwargs):
self.house = LegoHouse(*args, **kwargs)
def build(self):
""" Build a lego house instance and return it """
self.build_rooms()
self.build_porches()
return self.house
def build_rooms(self):
""" Method to build rooms """
for i in range(self.house.nrooms):
room = Room(self.house.nwindows)
self.house.add_room(room)
def build_porches(self):
""" Method to build porches """
for i in range(self.house.nporches):
porch = Porch(1)
self.house.add_porch(porch)
class BudgetLegoHouseBuilder(LegoHouseBuilder):
""" Builder building budget lego house with 1 room and no porch and rooms having 1 window """
def __init__(self):
self.house = LegoHouse(nrooms=1, nporches=0, nwindows=1)
class SmallLegoHouseBuilder(LegoHouseBuilder):
""" Builder building small lego house with 1 room and 1 porch and rooms having 2 windows """
def __init__(self):
self.house = LegoHouse(nrooms=2, nporches=1, nwindows=2)
class NorthFacingHouseBuilder(LegoHouseBuilder):
""" Builder building all rooms and porches facing North """
def build_rooms(self):
for i in range(self.house.nrooms):
room = Room(self.house.nwindows, direction='N')
self.house.add_room(room)
def build_porches(self):
for i in range(self.house.nporches):
porch = Porch(1, direction='N')
self.house.add_porch(porch)
class NorthFacingSmallHouseBuilder(NorthFacingHouseBuilder, SmallLegoHouseBuilder):
pass
if __name__ == "__main__":
bbuilder = BudgetLegoHouseBuilder()
print(bbuilder.build())
sbuilder = SmallLegoHouseBuilder()
print(sbuilder.build())
nbuilder = NorthFacingHouseBuilder(nrooms=2, nporches=1, nwindows=1)
print(nbuilder.build())
print(NorthFacingSmallHouseBuilder().build())