Skip to content

Instantly share code, notes, and snippets.

@rbwestmoreland
Created December 30, 2011 16:10
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 rbwestmoreland/1540447 to your computer and use it in GitHub Desktop.
Save rbwestmoreland/1540447 to your computer and use it in GitHub Desktop.
Properly Implementing the Proxy Pattern
using System;
/// <summary>
/// The actual object.
/// </summary>
public class ConcreteObject : IObject
{
public void DoWork()
{
}
}
using System;
/// <summary>
/// The proxy for the actual object.
/// <para>The Proxy pattern is a structural pattern,
/// whose purpose is to provide a surrogate or placeholder
/// for another object to control access to it.</para>
/// </summary>
public class ConcreteObjectProxy : IObject // FootNote 1
{
private Lazy<IObject> Instance = new Lazy<IObject>(); // FootNote 2
public void DoWork()
{
// Add additional responsibilities, before and/or after.
// (e.g. make thread-safe, check permissions, etc.)
Instance.Value.DoWork();
}
}
#region FootNotes
// 1) Optionally, this class could also implement the IDisposable interface.
// (http://conventionalcoding.betobates.com/properly-implementing-idisposable)
// 2) Lazy initialization.
// (http://en.wikipedia.org/wiki/Lazy_initialization)
// Good when the actual object is resource heavy, or when resources are scarce
// within the system.
#endregion FootNotes
using System;
/// <summary>
/// The common interface shared by the actual object and the proxy object.
/// </summary>
public interface IObject
{
void DoWork();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment