前幾天我遇到了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