by geoffrey
26. January 2010 18:26
This the second part of the series of Coding Kata’s. Here we’ll explore the Decorator pattern.

This is the user story:
You need to send alerts to your customers when a new product is available.
An Alert is represented by the Alert Class:
public class Alert
{
public String DestinationAddress { get; set; }
public String SenderAddress { get; set; }
public String Content { get; set; }
public string Log { get; set; }
}
Classes used to send the Alerts implement the following interface:
public interface ISender
{
void Send(Alert alert);
}
- Create an Alerter class that send the appropriate alerts.
- Alerts are always send by e-mail.
- Customers can also subscribe to receive alerts via sms and/or through messenger.
- Use the Decorator pattern to configure the Alerter
You do not need to use real infrastructure code to send the alert, just append a string that contains the Sender through which the message was send.
->Append the following text to the Alert.Log when the message is send by:
- e-mail : “Message was send by e-mail”
- sms : “Message was send by sms”
- messenger: “Message was send by messenger”
Show Solution Hide Solution
using System;
using NUnit.Framework;
namespace Kata2
{
#region Tests
[TestFixture]
public class AlerterTest
{
[Test]
public void SendAlert_ThroughEmailSender_SendEmail()
{
//Arrenge
var sender = new EmailSender();
var subject = new Alerter(sender);
var message = new Alert();
//Act
subject.Send(message);
//Assert
Assert.AreEqual(LogConstants.SendByEmail, message.Log);
}
[Test]
public void SendAlert_ThroughSmsSender_SendEmailAndSms()
{
//Arrenge
var emailSender = new EmailSender();
var smsSender = new SmsSender(emailSender);
var subject = new Alerter(smsSender);
var message = new Alert();
//Act
subject.Send(message);
//Assert
Assert.AreEqual(LogConstants.SendByEmail + LogConstants.SendBySMS, message.Log);
}
[Test]
public void SendAlert_ThroughMessengerSenderWithEmailSender_SendEmailAndMessengerAlert()
{
//Arrenge
var emailSender = new EmailSender();
var messengerSender = new MessengerSender(emailSender);
var subject = new Alerter(messengerSender);
var message = new Alert();
//Act
subject.Send(message);
//Assert
Assert.AreEqual(
LogConstants.SendByEmail + LogConstants.SendByMessenger
, message.Log
);
}
[Test]
public void SendAlert_ThroughMessengerSenderWithSmsSender_SendEmailAndSmsAndMessengerAlert()
{
//Arrenge
var emailSender = new EmailSender();
var smsSender = new SmsSender(emailSender);
var messengerSender = new MessengerSender(smsSender);
var subject = new Alerter(messengerSender);
var message = new Alert();
//Act
subject.Send(message);
//Assert
Assert.AreEqual(
LogConstants.SendByEmail + LogConstants.SendBySMS + LogConstants.SendByMessenger
, message.Log
);
}
}
#endregion
#region Decorators
public class MessengerSender: ISender
{
private ISender _sender;
public MessengerSender(ISender sender)
{
_sender = sender;
}
public void Send(Alert message)
{
_sender.Send(message);
//Here we would send a messenger alert
message.Log += LogConstants.SendByMessenger;
}
}
public class SmsSender : ISender
{
private ISender _sender;
public SmsSender(ISender sender)
{
_sender = sender;
}
public void Send(Alert message)
{
_sender.Send(message);
//Here we would send the SMS
message.Log += LogConstants.SendBySMS;
}
}
#endregion
#region Component
public class EmailSender : ISender
{
public void Send(Alert message)
{
//Here we would send an e-mail
message.Log = LogConstants.SendByEmail;
}
}
public interface ISender
{
void Send(Alert message);
}
#endregion
#region Client
public class Alerter
{
private ISender _sender;
public Alerter(ISender sender)
{
_sender = sender;
}
public void Send(Alert message)
{
_sender.Send(message);
}
}
#endregion
#region Other
public class Alert
{
public String DestinationAddress { get; set; }
public String SenderAddress { get; set; }
public String Content { get; set; }
public string Log { get; set; }
}
public class LogConstants
{
public const string SendByEmail = "Message was send by e-mail /n";
public const string SendBySMS = "Message was send by sms /n";
public const string SendByMessenger = "Message was send by messenger /n";
}
#endregion
}
73b5e63f-e5de-4ad6-8045-3dfcc7164768|0|.0
Tags:
by geoffrey
25. January 2010 20:19
This post is the first in a series dedicated to Coding Kata’s and design patterns.
Because I’m not always able to remember all the patterns I decided to learn by practice.
Every week I will try to create a Kata with a simple problem that has to be resolved with one of the Gov’s design patterns.
Because I want to start smoothly we’ll start with one of the simplest pattern -> the Abstract Factory.

Kata1 ->
- A product has a property Name.
- Because the Name is used as an identifier it can’t be changed.
- We need to be able to construct 2 products with following names: Product1, Product2.
- Use the abstract factory pattern so that you’re able to create Products.
This is my solution for Kata1.
47620a66-1b8a-49dc-8f2d-1d310319045f|0|.0
Tags:
by geoffrey
25. January 2010 16:54
Here under you can find two usefull ressources to prepare for the exam:
- The Wrox book: Professional Sharepoint 2007 Development
- The Blog of: Adam Roderick
By focusing primarily on these two resources I managed to pass the Sharepoint Certification with what could be considered a ‘high score’.

98ab6ca6-3d89-4451-a44f-0d9503d2eac3|0|.0
Tags: