Design Pattern Examples
Overview of object-oriented design patterns
Decorator_Classes.cs
Go to the documentation of this file.
1
7
8
using
System;
9
using
System.Xml.Linq;
10
11
namespace
DesignPatternExamples_csharp
12
{
13
14
//########################################################################
15
//########################################################################
16
17
22
public
interface
IRenderElement
23
{
28
string
Render
();
29
}
30
31
32
//########################################################################
33
//########################################################################
34
35
40
public
class
Decorator
:
IRenderElement
41
{
42
IRenderElement
_wrappedElement
;
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
79
public
class
WhiteBackgroundDecorator
:
Decorator
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
111
public
class
UnderlineDecorator
:
Decorator
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
144
public
class
RedForegroundDecorator
:
Decorator
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
182
public
class
TextElement
:
IRenderElement
183
{
184
string
_elementText
;
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
}
DesignPatternExamples_csharp.Decorator
Represents the base class of all decorators and is responsible for handling the wrapped element being...
Definition:
Decorator_Classes.cs:41
DesignPatternExamples_csharp.Decorator.Decorator
Decorator(IRenderElement wrappedElement)
Constructor.
Definition:
Decorator_Classes.cs:51
DesignPatternExamples_csharp.Decorator.Render
virtual string Render()
Retrieves the rendering of the wrapped element.
Definition:
Decorator_Classes.cs:65
DesignPatternExamples_csharp.Decorator._wrappedElement
IRenderElement _wrappedElement
Definition:
Decorator_Classes.cs:42
DesignPatternExamples_csharp.RedForegroundDecorator
Represents the RedForeground decorator, which renders the wrapped content as red text.
Definition:
Decorator_Classes.cs:145
DesignPatternExamples_csharp.RedForegroundDecorator.Render
override string Render()
Render the wrapped element with decorations.
Definition:
Decorator_Classes.cs:162
DesignPatternExamples_csharp.RedForegroundDecorator.RedForegroundDecorator
RedForegroundDecorator(IRenderElement element)
Constructor that wraps the specified element.
Definition:
Decorator_Classes.cs:153
DesignPatternExamples_csharp.TextElement
Represents the core element that can be decorated. Note that this class implements the IRenderElement...
Definition:
Decorator_Classes.cs:183
DesignPatternExamples_csharp.TextElement.Render
string Render()
Render this element as a string.
Definition:
Decorator_Classes.cs:201
DesignPatternExamples_csharp.TextElement._elementText
string _elementText
Definition:
Decorator_Classes.cs:184
DesignPatternExamples_csharp.TextElement.TextElement
TextElement(string element)
Constructor.
Definition:
Decorator_Classes.cs:190
DesignPatternExamples_csharp.UnderlineDecorator
Represents the Underline decorator, which underlines the wrapped content.
Definition:
Decorator_Classes.cs:112
DesignPatternExamples_csharp.UnderlineDecorator.Render
override string Render()
Render the wrapped element with decorations.
Definition:
Decorator_Classes.cs:129
DesignPatternExamples_csharp.UnderlineDecorator.UnderlineDecorator
UnderlineDecorator(IRenderElement element)
Constructor that wraps the specified element.
Definition:
Decorator_Classes.cs:120
DesignPatternExamples_csharp.WhiteBackgroundDecorator
Represents the WhiteBackground decorator, which changes the background color of the wrapped element t...
Definition:
Decorator_Classes.cs:80
DesignPatternExamples_csharp.WhiteBackgroundDecorator.Render
override string Render()
Render the wrapped element with decorations.
Definition:
Decorator_Classes.cs:96
DesignPatternExamples_csharp.WhiteBackgroundDecorator.WhiteBackgroundDecorator
WhiteBackgroundDecorator(IRenderElement element)
Constructor that wraps the specified element.
Definition:
Decorator_Classes.cs:88
DesignPatternExamples_csharp.IRenderElement
Represents an element that can be rendered in text. All decorators and the core element class impleme...
Definition:
Decorator_Classes.cs:23
DesignPatternExamples_csharp.IRenderElement.Render
string Render()
Render this element as a string.
DesignPatternExamples_csharp
The namespace containing all Design Pattern Examples implemented in C#.
Definition:
Adapter_BackEndFunctions.cs:19
csharp
Decorator_Classes.cs
Generated on Tue Aug 29 2023 19:47:44 for Design Pattern Examples by
1.9.6