forked from RefactoringGuru/design-patterns-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
139 lines (100 loc) · 5.68 KB
/
main.py
File metadata and controls
139 lines (100 loc) · 5.68 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
"""
EN: Bridge Design Pattern
Intent: Lets you split a large class or a set of closely related classes into
two separate hierarchies—abstraction and implementation—which can be developed
independently of each other.
A
/ \ A N
Aa Ab ===> / \ / \
/ \ / \ Aa(N) Ab(N) 1 2
Aa1 Aa2 Ab1 Ab2
RU: Паттерн Мост
Назначение: Разделяет один или несколько классов на две отдельные иерархии —
абстракцию и реализацию, позволяя изменять их независимо друг от друга.
A
/ \ A N
Aa Ab ===> / \ / \
/ \ / \ Aa(N) Ab(N) 1 2
Aa1 Aa2 Ab1 Ab2
"""
from __future__ import annotations
from abc import ABC, abstractmethod
class Abstraction:
"""
EN: The Abstraction defines the interface for the "control" part of the two
class hierarchies. It maintains a reference to an object of the
Implementation hierarchy and delegates all of the real work to this object.
RU: Абстракция устанавливает интерфейс для «управляющей» части двух иерархий
классов. Она содержит ссылку на объект из иерархии Реализации и делегирует
ему всю настоящую работу.
"""
def __init__(self, implementation: Implementation) -> None:
self.implementation = implementation
def operation(self) -> str:
return (f"Abstraction: Base operation with:\n"
f"{self.implementation.operation_implementation()}")
class ExtendedAbstraction(Abstraction):
"""
EN: You can extend the Abstraction without changing the Implementation
classes.
RU: Можно расширить Абстракцию без изменения классов Реализации.
"""
def operation(self) -> str:
return (f"ExtendedAbstraction: Extended operation with:\n"
f"{self.implementation.operation_implementation()}")
class Implementation(ABC):
"""
EN: The Implementation defines the interface for all implementation classes.
It doesn't have to match the Abstraction's interface. In fact, the two
interfaces can be entirely different. Typically the Implementation interface
provides only primitive operations, while the Abstraction defines higher-
level operations based on those primitives.
RU: Реализация устанавливает интерфейс для всех классов реализации. Он не
должен соответствовать интерфейсу Абстракции. На практике оба интерфейса
могут быть совершенно разными. Как правило, интерфейс Реализации
предоставляет только примитивные операции, в то время как Абстракция
определяет операции более высокого уровня, основанные на этих примитивах.
"""
@abstractmethod
def operation_implementation(self) -> str:
pass
"""
EN: Each Concrete Implementation corresponds to a specific platform and
implements the Implementation interface using that platform's API.
RU: Каждая Конкретная Реализация соответствует определённой платформе и
реализует интерфейс Реализации с использованием API этой платформы.
"""
class ConcreteImplementationA(Implementation):
def operation_implementation(self) -> str:
return "ConcreteImplementationA: Here's the result on the platform A."
class ConcreteImplementationB(Implementation):
def operation_implementation(self) -> str:
return "ConcreteImplementationB: Here's the result on the platform B."
def client_code(abstraction: Abstraction) -> None:
"""
EN: Except for the initialization phase, where an Abstraction object gets
linked with a specific Implementation object, the client code should only
depend on the Abstraction class. This way the client code can support any
abstraction-implementation combination.
RU: За исключением этапа инициализации, когда объект Абстракции связывается
с определённым объектом Реализации, клиентский код должен зависеть только от
класса Абстракции. Таким образом, клиентский код может поддерживать любую
комбинацию абстракции и реализации.
"""
# ...
print(abstraction.operation(), end="")
# ...
if __name__ == "__main__":
"""
EN: The client code should be able to work with any pre-configured
abstraction-implementation combination.
RU: Клиентский код должен работать с любой предварительно сконфигурированной
комбинацией абстракции и реализации.
"""
implementation = ConcreteImplementationA()
abstraction = Abstraction(implementation)
client_code(abstraction)
print("\n")
implementation = ConcreteImplementationB()
abstraction = ExtendedAbstraction(implementation)
client_code(abstraction)