MCPcopy Create free account
hub / github.com/RefactoringGuru/design-patterns-python / Component

Class Component

src/Composite/Conceptual/main.py:19–90  ·  view source on GitHub ↗

EN: The base Component class declares common operations for both simple and complex objects of a composition. RU: Базовый класс Компонент объявляет общие операции как для простых, так и для сложных объектов структуры.

Source from the content-addressed store, hash-verified

17
18
19class Component(ABC):
20 """
21 EN: The base Component class declares common operations for both simple and
22 complex objects of a composition.
23
24 RU: Базовый класс Компонент объявляет общие операции как для простых, так и
25 для сложных объектов структуры.
26 """
27
28 @property
29 def parent(self) -> Component:
30 return self._parent
31
32 @parent.setter
33 def parent(self, parent: Component):
34 """
35 EN: Optionally, the base Component can declare an interface for setting
36 and accessing a parent of the component in a tree structure. It can also
37 provide some default implementation for these methods.
38
39 RU: При необходимости базовый Компонент может объявить интерфейс для
40 установки и получения родителя компонента в древовидной структуре. Он
41 также может предоставить некоторую реализацию по умолчанию для этих
42 методов.
43 """
44
45 self._parent = parent
46
47 """
48 EN: In some cases, it would be beneficial to define the child-management
49 operations right in the base Component class. This way, you won't need to
50 expose any concrete component classes to the client code, even during the
51 object tree assembly. The downside is that these methods will be empty for
52 the leaf-level components.
53
54 RU: В некоторых случаях целесообразно определить операции управления
55 потомками прямо в базовом классе Компонент. Таким образом, вам не нужно
56 будет предоставлять конкретные классы компонентов клиентскому коду, даже во
57 время сборки дерева объектов. Недостаток такого подхода в том, что эти
58 методы будут пустыми для компонентов уровня листа.
59 """
60
61 def add(self, component: Component) -> None:
62 pass
63
64 def remove(self, component: Component) -> None:
65 pass
66
67 def is_composite(self) -> bool:
68 """
69 EN: You can provide a method that lets the client code figure out
70 whether a component can bear children.
71
72 RU: Вы можете предоставить метод, который позволит клиентскому коду
73 понять, может ли компонент иметь вложенные объекты.
74 """
75
76 return False

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected