-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesign pattern_SOLID
243 lines (186 loc) · 7.12 KB
/
design pattern_SOLID
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
You have created a set of services that implement the same API, but depending on the source (i.e. where the data is read from), it implements different business logic.
How would you do this considering the Liskov Substitution principle?
Make the caller unaware of the reader they are using. In fact abstract the reader through an abstract Reader class
from which the other classes are derived and only read through the abstracted memthods.
factory method
- helper:
class ShapeType(Enum) # self-documenting
class ShapeContext # container store to hold factory parameters
- main
class Shape(ABC) # abstract class
class ShapeFactory # create concret classes
class Circle(Shape) # concret class
class Rectangle(Shape) # concret class
from abc import ABC, abstractmethod
class Computer:
def __init__(self,processor=None,memory=None,storage=None,graphics_card=None,operating_system=None,extras=None):
# Initialize the attributes
self.processor = processor
self.memory = memory
self.storage = storage
self.graphics_card = graphics_card
self.operating_system = operating_system
self.extras = extras
class ComputerBuilder(ABC):
@abstractmethod
def add_processor(self):
pass
@abstractmethod
def add_memory(self):
pass
@abstractmethod
def add_storage(self):
pass
@abstractmethod
def add_graphics_card(self):
pass
@abstractmethod
def add_operating_system(self):
pass
@abstractmethod
def add_extras(self):
pass
class CustomComputerBuilder(ComputerBuilder):
def __init__(self):
# Initialize a Computer object
self.computer = Computer()
# Override abstract methods and set Computer attributes
def add_processor(self,value):
self.computer.processor = value
def add_memory(self,value):
self.computer.memory = value
def add_storage(self,value):
self.computer.storage = value
def add_graphics_card(self,value):
self.computer.graphics_card = value
def add_operating_system(self,value):
self.computer.operating_system = value
def add_extras(self,value):
self.computer.extras = value
class ComputerDirector:
def __init__(self, builder):
# Initialize the builder instance
self.builder = builder
def build_computer(self, specs):
# Call the add_* methods of the builder with the specs
# self.builder.add_processor(specs['processor'])
# self.builder.add_memory(specs['memory'])
# self.builder.add_storage(specs['storage'])
# self.builder.add_graphics_card(specs['graphics_card'])
# self.builder.add_operating_system(specs['operating_system'])
# self.builder.add_extras(specs['extras'])
for i,j in specs.items():
getattr(self.builder,f'add_{i}')(j)
# Helper function to test the computer building process
def test_computer_building(specs, expected_output):
builder = CustomComputerBuilder()
director = ComputerDirector(builder)
director.build_computer(specs)
computer = builder.computer
assert computer.__dict__ == expected_output, f"Expected {expected_output}, but got {computer.__dict__}"
# Test cases
test_specs = {
'processor': 'Intel Core i5',
'memory': '8GB',
'storage': '512GB SSD',
'graphics_card': 'Integrated',
'operating_system': 'Windows 11',
'extras': ['Wi-Fi']
}
expected_output = {
'processor': 'Intel Core i5',
'memory': '8GB',
'storage': '512GB SSD',
'graphics_card': 'Integrated',
'operating_system': 'Windows 11',
'extras': ['Wi-Fi']
}
test_computer_building(test_specs, expected_output)
print("All tests passed!")
**********************************************************************************************************************************************************************************
State Design Pattern
from abc import ABC, abstractmethod
# Step 1: Define the abstract base class TicketState
class TicketState(ABC):
@abstractmethod
def assign(self, ticket):
pass
@abstractmethod
def resolve(self, ticket):
pass
@abstractmethod
def close(self, ticket):
pass
# Step 2: Implement the concrete state classes
class NewState(TicketState):
def assign(self, ticket):
# Implement the behavior for assigning a new ticket
ticket.state = AssignedState()
print("Ticket has been assigned.")
def resolve(self, ticket):
# Implement the behavior for resolving a new ticket
print("Cannot resolve a new ticket. Assign it first.")
def close(self, ticket):
# Implement the behavior for closing a new ticket
ticket.state = ClosedState()
print("Ticket has been closed.")
# Implement the other concrete state classes: AssignedState, ResolvedState, and ClosedState
class AssignedState(TicketState):
def assign(self, ticket):
# Implement the behavior for assigning a new ticket
print("Ticket is already assigned.")
def resolve(self, ticket):
# Implement the behavior for resolving a new ticket
ticket.state = ResolvedState()
print("Ticket has been resolved.")
def close(self, ticket):
# Implement the behavior for closing a new ticket
ticket.state = ClosedState()
print("Ticket has been closed.")
class ResolvedState(TicketState):
def assign(self, ticket):
# Implement the behavior for assigning a new ticket
print("Cannot assign a resolved ticket.")
def resolve(self, ticket):
# Implement the behavior for resolving a new ticket
print("Ticket is already resolved.")
def close(self, ticket):
# Implement the behavior for closing a new ticket
ticket.state = ClosedState()
print("Ticket has been closed.")
class ClosedState(TicketState):
def assign(self, ticket):
# Implement the behavior for assigning a new ticket
print("Cannot assign a closed ticket.")
def resolve(self, ticket):
# Implement the behavior for resolving a new ticket
print("Cannot resolve a closed ticket.")
def close(self, ticket):
# Implement the behavior for closing a new ticket
print("Ticket is already closed.")
# Step 3: Implement the Ticket class
class Ticket:
def __init__(self):
# Initialize the ticket's state attribute with an instance of the NewState class
self.state = NewState()
def assign(self):
# Delegate the assign method call to the current state object
self.state.assign(self)
def resolve(self):
# Delegate the resolve method call to the current state object
self.state.resolve(self)
def close(self):
# Delegate the close method call to the current state object
self.state.close(self)
# Step 4: Test the behavior of the ticket and its state transitions
def main():
ticket = Ticket()
# Test the initial state and transitions
ticket.assign()
ticket.resolve()
ticket.close()
# Test invalid transitions
ticket.assign()
ticket.resolve()
if __name__ == "__main__":
main()