Design Pattern Examples
Overview of object-oriented design patterns
Decorator_Exercise.cs
Go to the documentation of this file.
1
5
6using System;
7
9{
21 internal class Decorator_Exercise
22 {
26 // ! [Using Decorator in C#]
27 public void Run()
28 {
29 Console.WriteLine();
30 Console.WriteLine("Decorator Exercise");
31 IRenderElement baseElement = new TextElement("This is raw text");
32
33 // Wrap the base element in three decorators.
34 IRenderElement wrappedElement =
37 new RedForegroundDecorator(baseElement)));
38
39 // Now render the elements to the console.
40 Console.WriteLine(" base Text element: \"{0}\"", baseElement.Render());
41 Console.WriteLine(" Decorated element: \"{0}\"", wrappedElement.Render());
42 Console.WriteLine(" Done.");
43 }
44 // ! [Using Decorator in C#]
45 }
46}
Example of using the Decorator Pattern in C#.
void Run()
Executes the example for the Decorator Pattern in C#.
Represents the RedForeground decorator, which renders the wrapped content as red text.
Represents the core element that can be decorated. Note that this class implements the IRenderElement...
Represents the Underline decorator, which underlines the wrapped content.
Represents the WhiteBackground decorator, which changes the background color of the wrapped element t...
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#.