Design Pattern in .NET 101 - Decorator Pattern (Structural Pattern)
By admin on Jan 9, 2008 in .NET, Programming, Structural
Decorator Pattern attaches additional responsibilities to an object dynamically and provides a flexible alternative to subclassing for extending functionality.
E.g.
BaseMessage is defined
using System;
using System.Collections.Generic;
using System.Text;
namespace DecoratorPattern
{
abstract class BaseMessage
{
private string sender;
private string recipient;
public string Sender
{
get
{
return this.sender;
}
set
{
this.sender = value;
}
}
public string Recipient {
get
{
return this.recipient;
}
set
{
this.recipient = value;
}
}
public abstract void Send();
}
}
Both SMS and MMS inherites BaseMessage
using System;
using System.Collections.Generic;
using System.Text;
namespace DecoratorPattern
{
class SMS: BaseMessage
{
private string message;
public SMS(string sender, string recipient, string message)
{
this.Sender = sender;
this.Recipient = recipient;
this.Message = message;
}
public string Message
{
get
{
return this.message;
}
set
{
this.message = value;
}
}
public override void Send()
{
Console.WriteLine("Send to SMSC.");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace DecoratorPattern
{
class MMS:BaseMessage
{
private byte[] image;
public MMS(string sender, string recipient, byte[] image)
{
this.Sender = sender;
this.Recipient = recipient;
this.Image = image;
}
public byte[] Image
{
get
{
return this.image;
}
set
{
this.image = value;
}
}
public override void Send()
{
Console.WriteLine("Send to MMSC.");
}
}
}
To add additional behaviors to the class, define Decorator class which extends BaseMessage also.
using System;
using System.Collections.Generic;
using System.Text;
namespace DecoratorPattern
{
class Decorator: BaseMessage
{
protected BaseMessage message;
public Decorator(BaseMessage message)
{
this.message = message;
}
public override void Send()
{
message.Send();
}
}
}
The additional processing is defined in MessageProcessor which extends Decorator
using System;
using System.Collections.Generic;
using System.Text;
namespace DecoratorPattern
{
class MessageProcessor:Decorator
{
private string serviceProvider;
public MessageProcessor(BaseMessage message)
: base(message)
{
}
public string ServiceProvider
{
get
{
return this.serviceProvider;
}
set
{
this.serviceProvider = value;
}
}
public void PreProcess()
{
Console.WriteLine("Preprocessing message.");
}
public void PostProcess()
{
Console.WriteLine("Post processing message.");
}
public override void Send()
{
PreProcess();
base.Send();
PostProcess();
}
}
}
To test it
using System;
using System.Collections.Generic;
using System.Text;
namespace DecoratorPattern
{
class Program
{
static void Main(string[] args)
{
// Create SMS
SMS sms = new SMS("123", "456", "test message");
sms.Send();
// Create MMS
MMS mms = new MMS("123", "456", new byte[] { 1, 2 });
mms.Send();
// Set service provider, add pre and post post processing
MessageProcessor messageProcessor
= new MessageProcessor(sms);
messageProcessor.ServiceProvider = "telco1";
messageProcessor.Send();
// Wait for user
Console.Read();
}
}
}
The output
Send to SMSC. Send to MMSC. Preprocessing message. Send to SMSC. Post processing message.
