Created
December 16, 2015 06:59
-
-
Save jwChung/0be725f73380f74e7988 to your computer and use it in GitHub Desktop.
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
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