我正在寻找一个基类的设计模式,它强制所有派生类实现所有基类方法,而派生类中的方法具有不同的返回类型.该场景是一个通用的MemoryObject类(接口/抽象),保留和返回内存中的任何类型的对象.但派生类(static/sealed/singleton)应该返回正确的类型.
因此要求是 - 强制所有派生类实现基类的所有属性和方法 - 派生类中的方法必须返回正确的类型
这是一些示例代码,不可编译,但为了说明:
public interface MemoryObject
{
object FromMemory { get; }
//... other properties and methods
}
public sealed class MemoryObjectA : MemoryObject
{
public static List<string> FromMemory
{
get { return new List<string>(); }
}
//... other properties and methods
}
public sealed class MemoryObjectB : MemoryObject
{
public static DataTable FromMemory
{
get { return new DataTable(); }
}
//... other properties and methods
}
Run Code Online (Sandbox Code Playgroud)
提前感谢您的建议!