-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLecture_12_adapter.py
More file actions
186 lines (136 loc) · 4.5 KB
/
Copy pathLecture_12_adapter.py
File metadata and controls
186 lines (136 loc) · 4.5 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# 12 Design Patterns II
# Adapter
class ICell:
def get_left_wall(self): pass
def get_right_wall(self): pass
def get_up_wall(self): pass
def get_down_wall(self): pass
class ILabyrinth:
def get_labyrinth_dict(self): pass
class Cell(ICell):
def __init__(self, l, r, u, d):
self._left = l
self._right = r
self._up = u
self._down = d
def setLeftWall(self, opened):
self._left = opened
def setRightWall(self, opened):
self._right = opened
def setUpWall(self, opened):
self._up = opened
def setDownWall(self, opened):
self._down = opened
def get_left_wall(self):
return self._left
def get_right_wall(self):
return self._right
def get_up_wall(self):
return self._up
def get_down_wall(self):
return self._down
class Labyrinth(ILabyrinth):
def __init__(self, lab):
self._labyrinth_dict = lab
def get_labyrinth_dict(self):
return self._labyrinth_dict
def allWalls(cell):
if (isinstance(cell, ICell)):
return cell.get_left_wall() and cell.get_right_wall() and cell.get_up_wall() and cell.get_down_wall()
else:
raise Exception("Not a ICell")
class EnclosedCellsCounterVisitor:
def __init__(self):
self._enclosed_cells = 0
def reset(self):
self._enclosed_cells = 0
def visitCell(self, cell):
if (isinstance(cell, ICell)):
if (allWalls(cell)):
self._enclosed_cells += 1
else:
raise Exception("Invalid argument: not a cell")
def visitLabyrinth(self, lab):
if (isinstance(lab, ILabyrinth)):
for k, v in lab.get_labyrinth_dict().items():
self.visit(v)
else:
raise Exception("Invalid argument: not a labyrinth")
def visit(self, lab_item):
if (isinstance(lab_item, ILabyrinth)):
self.visitLabyrinth(lab_item)
if (isinstance(lab_item, ICell)):
self.visitCell(lab_item)
if (not isinstance(lab_item, ILabyrinth)
and not isinstance(lab_item, ICell)):
raise Exception("Invalid argument: not a cell, not a labyrinth")
def get_result(self):
return self._enclosed_cells
######################################33
internal_labyrinth_dict = {
(0,0): Cell(True, True, True, True),
(1,0): Cell(True, True, False, True),
(0,1): Cell(True, True, True, True),
(1,1): Cell(True, True, True, False)
}
labyrinth_dict = {
(0,0): Cell(True, True, True, True),
(1,0): Cell(True, True, True, True),
(0,1): Cell(True, True, True, True),
(1,1): Labyrinth(internal_labyrinth_dict)
}
labyrinth = Labyrinth(labyrinth_dict)
# print(labyrinth.get_labyrinth_dict())
#
# visitor = EnclosedCellsCounterVisitor()
# visitor.visit(labyrinth)
# print(visitor.get_result())
#####################################
# Suppose we can't change these classes:
class GCell:
def __init__(self, content):
self._content = content
class GCellLabyrinth:
def __init__(self, lab):
self._labyrinth_dict = lab
def get_labyrinth_dict(self):
return self._labyrinth_dict
########################################
# We need an adapter:
class GCellAdapter(ICell):
def __init__(self, gcell):
if (isinstance(gcell, GCell)):
self._gcell = gcell
else:
raise Exception("Not a GCell.")
def get_left_wall(self):
return self._gcell._content.count("l") == 1
def get_right_wall(self):
return self._gcell._content.count("r") == 1
def get_up_wall(self):
return self._gcell._content.count("u") == 1
def get_down_wall(self):
return self._gcell._content.count("d") == 1
class GCellLabyrinthAdapter(ILabyrinth):
def __init__(self, gcelllab):
if (isinstance(gcelllab, GCellLabyrinth)):
self._gcelllab = gcelllab
else:
raise Exception("Not a GCell labyrinth.")
def get_labyrinth_dict(self):
adapted_dict = dict()
for k, v in self._gcelllab.get_labyrinth_dict().items():
adapted_dict[k] = GCellAdapter(v)
return adapted_dict
##########################################
gcell_lab_dict = {
(0,0): GCell("lrud"),
(1,0): GCell("rd"),
(0,1): GCell("lrud"),
(1,1): GCell("lr")
}
gcell_lab = GCellLabyrinth(gcell_lab_dict)
adapted_gcell_lab = GCellLabyrinthAdapter(gcell_lab)
visitor = EnclosedCellsCounterVisitor()
visitor.visit(adapted_gcell_lab)
print(visitor.get_result())