Design Pattern Examples
Overview of object-oriented design patterns
Decorator_Classes.cs
Go to the documentation of this file.
1
7
8using System;
9using System.Xml.Linq;
10
12{
13
14 //########################################################################
15 //########################################################################
16
17
22 public interface IRenderElement
23 {
28 string Render();
29 }
30
31
32 //########################################################################
33 //########################################################################
34
35
41 {
43
51 public Decorator(IRenderElement wrappedElement)
52 {
53 if (wrappedElement == null)
54 {
55 throw new ArgumentNullException("wrappedElement",
56 "The element being decorated cannot be null.");
57 }
58 _wrappedElement = wrappedElement;
59 }
60
65 virtual public string Render()
66 {
67 return _wrappedElement.Render();
68 }
69 }
70
71 //########################################################################
72 //########################################################################
73
74
80 {
88 public WhiteBackgroundDecorator(IRenderElement element) : base(element)
89 {
90 }
91
96 public override string Render()
97 {
98 return String.Format("\x1b[47m{0}\x1b[49m", base.Render());
99 }
100 }
101
102
103 //########################################################################
104 //########################################################################
105
106
112 {
120 public UnderlineDecorator(IRenderElement element) : base(element)
121 {
122 }
123
124
129 override public string Render()
130 {
131 return String.Format("\x1b[4m{0}\x1b[24m", base.Render());
132 }
133 }
134
135
136 //########################################################################
137 //########################################################################
138
139
145 {
153 public RedForegroundDecorator(IRenderElement element) : base(element)
154 {
155 }
156
157
162 override public string Render()
163 {
164 return String.Format("\x1b[31m{0}\x1b[39m", base.Render());
165 }
166 }
167
168
169 //########################################################################
170 //########################################################################
171
172
183 {
185
190 public TextElement(string element)
191 {
192 _elementText = element;
193 }
194
195 #region IRenderElement Members
196
201 public string Render()
202 {
203 return _elementText;
204 }
205
206 #endregion
207 }
208}
Represents the base class of all decorators and is responsible for handling the wrapped element being...
Decorator(IRenderElement wrappedElement)
Constructor.
virtual string Render()
Retrieves the rendering of the wrapped element.
Represents the RedForeground decorator, which renders the wrapped content as red text.
override string Render()
Render the wrapped element with decorations.
RedForegroundDecorator(IRenderElement element)
Constructor that wraps the specified element.
Represents the core element that can be decorated. Note that this class implements the IRenderElement...
string Render()
Render this element as a string.
TextElement(string element)
Constructor.
Represents the Underline decorator, which underlines the wrapped content.
override string Render()
Render the wrapped element with decorations.
UnderlineDecorator(IRenderElement element)
Constructor that wraps the specified element.
Represents the WhiteBackground decorator, which changes the background color of the wrapped element t...
override string Render()
Render the wrapped element with decorations.
WhiteBackgroundDecorator(IRenderElement element)
Constructor that wraps the specified element.
Represents an element that can be rendered in text. All decorators and the core element class impleme...
string Render()
Render this element as a string.
The namespace containing all Design Pattern Examples implemented in C#.