-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLecture_11.py
159 lines (113 loc) · 3.3 KB
/
Lecture_11.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
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
# 11. Problems in Software Engineering
# ------------------------------------
- Requirements can be (mostly are) badly defined.
- Decisions are full of compromises (goals, requirements, simplicity)
- Tight deadlines
# OOP problems
- Multiple inheritance (anti-pattern)
- Buidling taxonomies
- Inheritance
- Long chain of inheritance (Violation of Demetra's law)
- Ellipse-circle problem (Square-rectangle problem)
# https://en.wikipedia.org/wiki/Circle%E2%80%93ellipse_problem
- Diamond problem (Violation of Liskov Substitution Principle) (SOLID: https://twitter.com/unclebobmartin/status/1253752012728131585)
- Public Morozov
class Vehicle:
startEngine():
klflfldlsf
class HasWeel:
pass
class Car(Vehicle, HasWeel):
startEngine()
class Airplane(Vehicle, HasWeel):
startEngine()
class WaterAirPlane(Airplane):
startEngine(self):
super()
# Vehicle -> Airplane -> WaterAirPlane
1. Interfaces
1a. Small, single base class
2. Composition (OOP composition)
class Weels:
pass
class Enigne:
start():
pass
class Vehicle:
def __init__(self, weels, engine):
self._weels = weels
self._engine = engine
self._weels_count = weels.count()
def startEngine(self):
if (self._engine is not None):
self._engine.start()
class Car:
def __init__(self):
self._vehicle = Vehicle(Weels(), Engine())
class Car(Vehicle):
def __init__(self):
super(Vehicle, self).__init__(Weels(), Engine())
class Airplane(Vehicle):
def __init__(self, waterSlidingPlanks):
...
# Ellipse-circle problem
# Ellipse 2 axis
# Circle is an ellipse with 2 equal axis
class Ellipse:
def __init__(self, x, y):
self._x = x
self._y = y
# def stretchX(self, dx):
# self._x = self._x + dx
# def stretchY(self, dy):
# self._x = self._x + dx
def stretch(self, dx, dy):
self._x = self._x + dx
self._x = self._x + dx
class Circle(Ellipse):
def __init__(self, d):
super(Ellipse, self).__init__(d, d)
def stretch(self, dx, dy):
if (dx != dy):
raise Exception('Invalid argument')
circle = Circle(10)
circle.stretchX(11000)
# Diamond problem
# abstract base class
class Vehicle:
@abstractmethod
def startEngine(): pass
class Car(Vehicle):
def startEngine(self):
#implement somehow
print('Engines started (car)')
class Scooter(Vehicle):
def startEngine(self):
#implement somehow
print('Engines started (scooter)')
class TukTuk(Car, Scooter):
def startEngine(self):
# ??? what startEngine to use?
super(Car, self).startEngine()
super(Scooter, self).startEngine()
# Long chain of inheritance
class A: pass
class B(A): pass
class C(B): pass
class D(C): pass
# Public Morozov
class Vehicle:
def __init__(self, weels, engine):
self.__weels = weels
self.__engine = engine
self.__usage_count = 0
def startEngine(self):
self.__usage_count += 1
self.__engine.start()
def service_needed(self):
return self.__engine.resource() <= self.__usage_count
class Car(Vehicle):
def __init__(self):
super(Vehicle, self).__init__(Weels(), Engine())
def startEngine(): # Public Morozov
self.__engine.start()