Skip to content

Instantly share code, notes, and snippets.

@sevperez
Created October 11, 2018 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sevperez/b183c8f224719b6fb556f1395386ffc1 to your computer and use it in GitHub Desktop.
Save sevperez/b183c8f224719b6fb556f1395386ffc1 to your computer and use it in GitHub Desktop.
using System;
namespace decorator_1
{
class Program
{
static void Main(string[] args)
{
var msg = "I love C-sharp";
var simpleMsg = new SimpleMessage(msg);
var excitedDecoratedMsg = new ExcitedMessageDecorator(simpleMsg);
var quizzicalDecoratedMsg = new QuizzicalMessageDecorator(simpleMsg);
var excitedAndQuizzicalDecoratedMsg = new QuizzicalMessageDecorator(excitedDecoratedMsg);
IMessage[] messages = { simpleMsg, excitedDecoratedMsg,
quizzicalDecoratedMsg, excitedAndQuizzicalDecoratedMsg };
foreach (IMessage m in messages)
{
m.PrintMessage();
}
// I love C-sharp
// I love C-sharp!!!
// I love C-sharp???
// I love C-sharp!!!???
simpleMsg.PrintMessage();
// I love C-sharp
}
}
public interface IMessage
{
string GetMessage();
void PrintMessage();
}
public class SimpleMessage : IMessage
{
private string Content;
public SimpleMessage(string content)
{
this.Content = content;
}
public string GetMessage()
{
return this.Content;
}
public void PrintMessage()
{
Console.WriteLine(this.GetMessage());
}
}
public abstract class MessageDecorator : IMessage
{
private IMessage Message;
public MessageDecorator(IMessage message)
{
this.Message = message;
}
public virtual string GetMessage()
{
return this.Message.GetMessage();
}
public virtual void PrintMessage()
{
this.Message.PrintMessage();
}
}
public class ExcitedMessageDecorator : MessageDecorator
{
public ExcitedMessageDecorator(IMessage message) : base(message) {}
public override string GetMessage()
{
return base.GetMessage() + "!!!";
}
public override void PrintMessage()
{
Console.WriteLine(this.GetMessage());
}
}
public class QuizzicalMessageDecorator : MessageDecorator
{
public QuizzicalMessageDecorator(IMessage message) : base(message) {}
public override string GetMessage()
{
return base.GetMessage() + "???";
}
public override void PrintMessage()
{
Console.WriteLine(this.GetMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment