常规界面:
public interface IComputation
{
void Reset();
float GetValue1();
float GetValue2();
}
通用接口:
public interface IComputation<T> : IComputation where T : IComputation
{
T Proxy { get; set; }
}
现在上课:
public abstract class Computation<T> : IComputation<T> where T : IComputation
{
public T Proxy { get; set; }
}
类 'ComputationCache' 是一个 'decorated' 计算:
internal abstract class ComputationCache<T> : IComputation where T : IComputation
{
public T Proxy { get; set; }
public float GetValue1()
{
bool isCached = //check cache.
if(!isCached)
{
//compute value
float value1 = Proxy.GetValue1();
//update cache
return value;
}
}
}
为了初始化装饰计算,我尝试了以下操作:
public ComputationCache(IComputation<T> proxy)
{
Proxy = (T) proxy;
proxy.Proxy = this;
}
...这给出了以下错误”:
无法将源类型“ComputationCache”转换为目标类型“T”。
有人可以评论是否更好地使用:
ComputationCache<T> : IComputation where T : IComputation
对比
ComputationCache<T> : IComputation<T> where T : IComputation