-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShape.py
More file actions
54 lines (43 loc) · 1.43 KB
/
Copy pathShape.py
File metadata and controls
54 lines (43 loc) · 1.43 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
"""Design and implement ShapeFactory class that generates different types of Shape
objects (Circle, Square, Rectangle) based on input parameters using Factory Design
Pattern."""
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def draw(self):
pass
class Circle(Shape):
def __init__(self,radius):
self.radius = radius
def draw(self):
return f"drawing a circle with radius {self.radius}"
class Square(Shape):
def __init__(self,side):
self.side = side
def draw(self):
return f"drawing a circle with side {self.side}"
class Rectangle(Shape):
def __init__(self,width,height):
self.width = width
self.height = height
def draw(self):
return f"drawing a circle with width {self.width} and height {self.height}"
class ShapeFactory:
def create_shape(self,shape_type,*args):
if shape_type == "Circle":
return Circle(*args)
elif shape_type == "Square":
return Square(*args)
elif shape_type == "Rectangle":
return Rectangle(*args)
else:
raise ValueError(f"unknown shape type:{shape_type}")
if __name__ == "__main__":
factory = ShapeFactory()
shapes = {
factory .create_shape("Circle" ,5),
factory .create_shape("Square" ,4),
factory .create_shape("Rectangle" ,3 , 6)
}
for Shape in shapes:
print(Shape.draw())