forked from gil9red/SimplePyScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_2.py
More file actions
90 lines (59 loc) · 1.98 KB
/
Copy pathexample_2.py
File metadata and controls
90 lines (59 loc) · 1.98 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'ipetrash'
# SOURCE: Design Patterns: Bridge — Мост
# SOURCE: https://ru.wikipedia.org/wiki/Мост_(шаблон_проектирования)
from abc import ABC, abstractmethod
class Drawer(ABC):
@abstractmethod
def draw_circle(self, x: int, y: int, radius: int):
pass
class SmallCircleDrawer(Drawer):
RADIUS_MULTIPLIER = 0.25
def draw_circle(self, x: int, y: int, radius: int):
print(f"Small circle center = {x},{y} radius = {radius * self.RADIUS_MULTIPLIER}")
class LargeCircleDrawer(Drawer):
RADIUS_MULTIPLIER = 10
def draw_circle(self, x: int, y: int, radius: int):
print(f"Large circle center = {x},{y} radius = {radius * self.RADIUS_MULTIPLIER}")
class Shape(ABC):
def __init__(self, drawer: Drawer):
self._drawer = drawer
@abstractmethod
def draw(self):
pass
@abstractmethod
def enlarge_radius(self, multiplier: int):
pass
class Circle(Shape):
def __init__(self, x: int, y: int, radius: int, drawer: Drawer):
super().__init__(drawer)
self._x = x
self._y = y
self._radius = radius
def draw(self):
self._drawer.draw_circle(self._x, self._y, self._radius)
def enlarge_radius(self, multiplier: int):
self._radius *= multiplier
def get_x(self) -> int:
return self._x
def get_y(self) -> int:
return self._y
def get_radius(self) -> int:
return self._radius
def set_x(self, x: int):
self._x = x
def set_y(self, y: int):
self._y = y
def set_radius(self, radius: int):
self._radius = radius
if __name__ == '__main__':
shapes = [
Circle(5, 10, 10, LargeCircleDrawer()),
Circle(20, 30, 100, SmallCircleDrawer()),
]
for x in shapes:
x.draw()
# Output
# Large circle center = 5,10 radius = 100
# Small circle center = 20,30 radius = 25.0