-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOpenClosedPrinciple.py
More file actions
144 lines (115 loc) · 2.75 KB
/
OpenClosedPrinciple.py
File metadata and controls
144 lines (115 loc) · 2.75 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
import math
# class Rectangle(object):
#
# def __init__(self, height, width):
# self._height = height
# self._width = width
#
# @property
# def height(self):
# return self._height
#
# @height.setter
# def height(self, height):
# self._height = height
#
# @property
# def width(self):
# return self._width
#
# @width.setter
# def width(self, width):
# self._width = width
#
# def __str__(self):
# pass
#
#
# class Circle(object):
#
# def __init__(self, radius):
# self._radius = radius
#
# @property
# def radius(self):
# return self._radius
#
# @radius.setter
# def radius(self, radius):
# self._radius = radius
# class AreaCalculator:
#
# def Area(self, shapes):
#
# area = 0
#
# area = shapes.width*shapes.height
# return area
#
#
# rect = Rectangle(12, 13)
# area = AreaCalculator()
# print(area.Area(rect))
# Let's say if Area is circle then you can do like below
# class AreaCalculator:
#
# def Area(self, shapes):
#
# area = 0
# if isinstance(shapes, Rectangle):
# area = shapes.width*shapes.height
# else:
# area = shapes.radius * shapes.radius * math.pi
# return area
# rect = Rectangle(12, 13)
# area = AreaCalculator()
# print(area.Area(rect))
#
# cir = Circle(3)
# print(area.Area(cir))
# Now if you have been asked for triangle area then you might have to change AreaCalculator again.
#A solution that abides by the Open/Closed Principle
#One way of solving this puzzle would be to create a base class for both rectangles and
#circles as well as any other shapes that Aldford can think of which defines an abstract method for calculating it’s area.
class Shape:
def area(self):
pass
class Rectangle(Shape):
def __init__(self, height, width):
self._height = height
self._width = width
@property
def height(self):
return self._height
@height.setter
def height(self, height):
self._height = height
@property
def width(self):
return self._width
@width.setter
def width(self, width):
self._width = width
def area(self):
area = self._width * self._height
return area
class Circle(Shape):
def __init__(self, radius):
self._radius = radius
@property
def radius(self):
return self._radius
@radius.setter
def radius(self, radius):
self._radius = radius
def area(self):
area = self._radius * self._radius * math.pi
return area
def Area(shapes):
area = 0;
area = shapes.area();
return area;
rect = Rectangle(2,4)
cir = Circle(3)
print(Area(rect))
print(Area(cir))