인터페이스를 통해 아래의 클래스 메서드를 노출하는 데 도움이 될 수 있습니까? 인터페이스를 통해 아래의 Cache 클래스 메소드를 사용할 수 있기를 원합니다. 기본적으로 공통 캐시 메소드가 정의 된 일반 인터페이스를 생성해야하며 해당 구현은 아래 클래스에서 제공됩니다.일반 인터페이스 구현
public class CacheStore
{
private Dictionary<string, object> _cache;
private object _sync;
public CacheStore()
{
_cache = new Dictionary<string, object>();
_sync = new object();
}
public bool Exists<T>(string key) where T : class
{
Type type = typeof(T);
lock (_sync)
{
return _cache.ContainsKey(type.Name + key);
}
}
public bool Exists<T>() where T : class
{
Type type = typeof(T);
lock (_sync)
{
return _cache.ContainsKey(type.Name);
}
}
public T Get<T>(string key) where T : class
{
Type type = typeof(T);
lock (_sync)
{
if (_cache.ContainsKey(key + type.Name) == false)
throw new ApplicationException(String.Format("An object with key '{0}' does not exists", key));
lock (_sync)
{
return (T)_cache[key + type.Name];
}
}
}
public void Add<T>(string key, T value)
{
Type type = typeof(T);
if (value.GetType() != type)
throw new ApplicationException(String.Format("The type of value passed to
cache {0} does not match the cache type {1} for key {2}",
value.GetType().FullName, type.FullName, key));
lock (_sync)
{
if (_cache.ContainsKey(key + type.Name))
throw new ApplicationException(String.Format("An object with key '{0}'
already exists", key));
lock (_sync)
{
_cache.Add(key + type.Name, value);
}
}
}
}
어떤 인터페이스입니까? 당신이 묻고있는 것이 명확하지 않습니다. –
메소드 서명을 인터페이스로 추출하고'public'을 제거하고 인터페이스에 이름을 지정하면 완료됩니다. –