Design Pattern Examples
Overview of object-oriented design patterns
decorator_exercise.py
Go to the documentation of this file.
6
7from .decorator_classes import WhiteBackgroundDecorator, UnderlineDecorator, RedForegroundDecorator, TextElement
8
9
10
19
20# ! [Using Decorator in Python]
22 print()
23 print("Decorator Exercise")
24
25 baseElement = TextElement("This is raw text")
26
27 # Wrap the base element in three decorators.
28 wrappedElement = \
31 RedForegroundDecorator(baseElement)
32 )
33 )
34
35 # Now render the elements to the console.
36 print(" base Text element: \"{0}\"".format(baseElement.Render()))
37 print(" Decorated element: \"{0}\"".format(wrappedElement.Render()))
38
39 print(" Done.")
40# ! [Using Decorator in Python]
Represents the RedForeground decorator, which renders the wrapped content as red text.
Represents the core element that can be decorated.
Represents the Underline decorator, which underlines the wrapped content.
Represents the WhiteBackground decorator, which changes the background color of the wrapped element t...
def Decorator_Exercise()
Example of using the Decorator Pattern.