Skip to content

Instantly share code, notes, and snippets.

@rbwestmoreland
Created December 29, 2011 16:38
Show Gist options
  • Save rbwestmoreland/1534885 to your computer and use it in GitHub Desktop.
Save rbwestmoreland/1534885 to your computer and use it in GitHub Desktop.
Properly Implementing the Decorator Pattern
using System;
/// <summary>
/// The object which adds responsibilities to
/// a base instance.
/// <para>The Decorator pattern is a structural pattern, whose
/// purpose is to attach additional responsibilities to
/// an object dynamically keeping the same interface. </para>
/// </summary>
public class BaseDecorator : IBase
{
private IBase Instance { get; set; }
public BaseDecorator(IBase instance)
{
Instance = instance;
}
public void DoWork()
{
//Perform default responsibilities.
Instance.DoWork();
// Add responsibilities here.
}
}
using System;
/// <summary>
/// The base interface.
/// </summary>
public interface IBase
{
void DoWork();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment