前几天我遇到了this blog post,它提供了一个可插拔的缓存管理器来使用不同的缓存提供程序。基本上,我们有一个ICacheProvider接口:此代码段是否使用Repository或Adapter模式?
public interface ICacheProvider
{
void Store(string key, object data);
void Destroy(string key);
T Get<T>(string key);
}
而且一类的CacheManager:
public class CacheManager
{
protected ICacheProvider _repository;
public CacheManager(ICacheProvider repository)
{
_repository = repository;
}
public void Store(string key, object data)
{
_repository.Store(key, data);
}
public void Destroy(string key)
{
_repository.Destroy(key);
}
public T Get<T>(string key)
{
return _repository.Get<T>(key);
}
}
最后,我们可以写我们自己的供应商:
public class SessionProvider : ICacheProvider
{
public void Store(string key, object data)
{
HttpContext.Current.Cache.Insert(key, data);
}
public void Destroy(string key)
{
HttpContext.Current.Cache.Remove(key);
}
public T Get<T>(string key)
{
T item = default(T);
if (HttpContext.Current.Cache[key] != null)
{
item = (T)HttpContext.Current.Cache[key];
}
return item;
}
}
嗯,我敢确保此代码使用基于http://www.dofactory.com/Patterns/PatternAdapter.aspx定义的适配器模式。
但似乎我们可以说它也使用Repository模式(除了与通常使用Repository模式的数据上的基本CRUD操作无关)。它在界面中包含了缓存管理器的一般内容。
我们可以说这段代码使用Repository模式和适配器模式吗?
你能改进这个答案,为什么?谢谢 – Drew 2015-12-17 05:23:51