July 2019
Intermediate to advanced
410 pages
10h 32m
English
To illustrate a structural pattern, let's take a closer look at the Decorator pattern by using an example. This example will print messages on a console application. First, a base message is defined with a corresponding interface:
interface IMessage{ void PrintMessage();}abstract class Message : IMessage{ protected string _text; public Message(string text) { _text = text; } abstract public void PrintMessage();}
The base class allows for the storage of a text string and requires that child classes implement the PrintMessage() method. This will then be extended into two new classes.
The first class is a SimpleMessage that writes the given text to the console:
class SimpleMessage : Message{ public SimpleMessage(string text) ...Read now
Unlock full access