Design Pattern Examples
Overview of object-oriented design patterns
decorator_classes.py
Go to the documentation of this file.
9
10from abc import ABC, abstractmethod
11
12
14
15
16
18class IRenderElement(ABC):
19
23 @abstractmethod
24 def Render(self) -> str:
25 pass
26
27
28
29
31
32
33
35class Decorator(IRenderElement):
36
37
42 def __init__(self, element : IRenderElement) -> None:
43 self._wrappedElement = element
44 if not self._wrappedElement:
45 raise ValueError("The element being decorated cannot be null.")
46
47
49
50
51
55 def Render(self) -> str:
56 return self._wrappedElement.Render()
57
58
59
61
62
63
66
71 def __init__(self, element: IRenderElement) -> None:
72 super().__init__(element)
73
74
75 # IRenderElement Members
76
77
81 def Render(self) -> str:
82 return "\x1b[47m{0}\x1b[49m".format(super().Render())
83
84
85
87
88
89
92
97 def __init__(self, element : IRenderElement) -> None:
98 super().__init__(element)
99
100 # IRenderElement Members
101
102
106 def Render(self) -> str:
107 return "\x1b[4m{0}\x1b[24m".format(super().Render())
108
109
110
112
113
114
115
118
123 def __init__(self, element : IRenderElement) -> None:
124 super().__init__(element)
125
126 # IRenderElement Members
127
128
132 def Render(self) -> str:
133 return "\x1b[31m{0}\x1b[39m".format(super().Render())
134
135
136
138
139
140
152
153
157 def __init__(self, element : str) -> None:
158 self._elementText = element;
159
160
162
163 # IRenderElement Members
164
165
169 def Render(self) -> str:
170 return self._elementText;
Represents the base class of all decorators and is responsible for handling the wrapped element being...
str Render(self)
Render the wrapped element with decorations.
None __init__(self, IRenderElement element)
Constructor.
_wrappedElement
Object being decorated by this class instance.
Represents an element that can be rendered in text.
Represents the RedForeground decorator, which renders the wrapped content as red text.
None __init__(self, IRenderElement element)
Constructor that wraps the specified element.
Represents the core element that can be decorated.
Represents the Underline decorator, which underlines the wrapped content.
None __init__(self, IRenderElement element)
Constructor that wraps the specified element.
Represents the WhiteBackground decorator, which changes the background color of the wrapped element t...
str Render(self)
Render the wrapped element with a white background.
None __init__(self, IRenderElement element)
Constructor that wraps the specified element.