-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_ocp.py
55 lines (33 loc) · 994 Bytes
/
01_ocp.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
# QUESTION: What happens when we add another shape?
class Circle():
radius: float
class Square():
length: float
PI = 3.14
class AreaCalculator():
def calculate_area(self, shape):
if isinstance(shape, Circle):
return shape.radius** 2 * PI
elif isinstance(shape, Square):
return shape.length ** 2
else:
-1
### Notes ###
# We'll have to add another elif branch
# Suggested Solution
from abc import ABCMeta, abstractmethod
class ShapeInterface(metaclass=ABCMeta):
@abstractmethod
def area() -> float:
raise NotImplementedError()
class Circle(ShapeInterface):
radius: float
def area(self) -> float:
return self.radius ** 2 * PI
class Square(ShapeInterface):
length: float
def area(self) -> float:
return self.length ** 2
class AreaCalculator():
def calculate_area(self, shape: ShapeInterface):
return shape.area()