Skip to content

Instantly share code, notes, and snippets.

@jwChung
Created December 16, 2015 06:59
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 jwChung/0be725f73380f74e7988 to your computer and use it in GitHub Desktop.
Save jwChung/0be725f73380f74e7988 to your computer and use it in GitHub Desktop.
public interface IFactory<T>
{
T Create();
}
public class CacheFactory<T> : IFactory<T>
{
private T cached;
private readonly object syncLook = new object();
public CacheFactory(IFactory<T> innerFactory)
{
if (innerFactory == null)
throw new ArgumentNullException(nameof(innerFactory));
this.InnerFactory = innerFactory;
}
public IFactory<T> InnerFactory { get; }
public T Create()
{
lock (this.syncLook)
{
if (object.Equals(cached, default(T)))
this.cached = this.InnerFactory.Create();
return this.cached;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment