Created
December 30, 2011 16:10
-
-
Save rbwestmoreland/1540447 to your computer and use it in GitHub Desktop.
Properly Implementing the Proxy Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
/// <summary> | |
/// The actual object. | |
/// </summary> | |
public class ConcreteObject : IObject | |
{ | |
public void DoWork() | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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