C#静态接口或抽象实现
C# static interface or abstract implementation
我有一个程序需要能够与多个平台交互,即 read/write 文件、read/write 数据库或 read/write 网络请求。平台接口是从配置中选择的,在应用运行时不会改变。我有一个单一的 read/write 接口 class,它由特定于平台的 classes 继承,因此它是从程序的其余部分中抽象出来的。
我的问题是我的框架中有 10 个 classes 需要使用这个接口。与其创建此 class 的多个实例,或将单个引用传递给每个 class,我认为将接口设为静态是有意义的。不幸的是,我刚刚了解到接口不能有静态方法,静态方法不能调用非静态方法,静态方法不能是抽象的。
谁能告诉我另一种处理这种情况的方法?
编辑:
感谢大家的意见,这是我根据Patrick Hofman给出的例子的解决方案(谢谢!)
interface TheInterface
{
void read();
void write();
}
public class X : TheInterface
{
public void read() { //do something }
public void write() { //do something }
}
public class Y : TheInterface
{
public void read() { //do something }
public void write() { //do something }
}
public class FileAccessor
{
public static TheInterface accessor;
public static TheInterface Accessor
{
get
{
if(accessor) return accessor;
}
}
}
这可以被任何 class 调用为:
static void Main(string[] args)
{
switch (Config.interface)
{
case "X":
FileAccessor.accessor = new Lazy<X>();
case "Y":
FileAccessor.accessor = new Lazy<Y>();
default:
throw new Lazy<Exception>("Unknown interface: " + Config.interface);
}
FileAccessor.Accessor.read();
}
的确,接口或抽象 类 本身不能 static
,但进一步的实现可以。另外,你可以使用单例模式让你的生活更轻松,并允许继承等。
public class X : ISomeInterface
{
private X() { }
public static X instance;
public static X Instance
{
get
{
return instance ?? (instance = new X());
}
}
}
或者,使用 Lazy<T>
:
public class X : ISomeInterface
{
private X() { }
public static Lazy<X> instanceLazy = new Lazy<X>(() => new X());
public static X Instance
{
get
{
return instance.Value;
}
}
}
你可以创建一个静态的Class,它有你的界面变量。
public static class StaticClass
{
public static ISomeInterface Interface;
}
现在您可以从 Framwork 中的任何位置访问实例
static void Main(string[] args)
{
StaticClass.Interface = new SomeClass();
}
免责声明:我是下述库的作者。
不知道对你有没有帮助,我已经写了a library (very early version yet) that allows you to define static interfaces, by defining normal interfaces and decorating their methods with an attribute named [Static]
,例如:
public interface IYourInterface
{
[Static]
void DoTheThing();
}
(请注意,您没有明确地将此接口添加到您的实现中。)
定义接口后,您可以使用您选择的任何有效实现从代码中实例化它:
return typeof(YourImplementation).ToStaticContract<IYourInterface>();
如果在 YourImplementation
中找不到方法,则此调用在运行时失败并出现异常。
如果找到方法并且此调用成功,则客户端代码可以像这样以多态方式调用您的静态方法:
IYourInterface proxy = GetAnImplementation();
proxy.DoTheThing();
我有一个程序需要能够与多个平台交互,即 read/write 文件、read/write 数据库或 read/write 网络请求。平台接口是从配置中选择的,在应用运行时不会改变。我有一个单一的 read/write 接口 class,它由特定于平台的 classes 继承,因此它是从程序的其余部分中抽象出来的。
我的问题是我的框架中有 10 个 classes 需要使用这个接口。与其创建此 class 的多个实例,或将单个引用传递给每个 class,我认为将接口设为静态是有意义的。不幸的是,我刚刚了解到接口不能有静态方法,静态方法不能调用非静态方法,静态方法不能是抽象的。
谁能告诉我另一种处理这种情况的方法?
编辑: 感谢大家的意见,这是我根据Patrick Hofman给出的例子的解决方案(谢谢!)
interface TheInterface
{
void read();
void write();
}
public class X : TheInterface
{
public void read() { //do something }
public void write() { //do something }
}
public class Y : TheInterface
{
public void read() { //do something }
public void write() { //do something }
}
public class FileAccessor
{
public static TheInterface accessor;
public static TheInterface Accessor
{
get
{
if(accessor) return accessor;
}
}
}
这可以被任何 class 调用为:
static void Main(string[] args)
{
switch (Config.interface)
{
case "X":
FileAccessor.accessor = new Lazy<X>();
case "Y":
FileAccessor.accessor = new Lazy<Y>();
default:
throw new Lazy<Exception>("Unknown interface: " + Config.interface);
}
FileAccessor.Accessor.read();
}
的确,接口或抽象 类 本身不能 static
,但进一步的实现可以。另外,你可以使用单例模式让你的生活更轻松,并允许继承等。
public class X : ISomeInterface
{
private X() { }
public static X instance;
public static X Instance
{
get
{
return instance ?? (instance = new X());
}
}
}
或者,使用 Lazy<T>
:
public class X : ISomeInterface
{
private X() { }
public static Lazy<X> instanceLazy = new Lazy<X>(() => new X());
public static X Instance
{
get
{
return instance.Value;
}
}
}
你可以创建一个静态的Class,它有你的界面变量。
public static class StaticClass
{
public static ISomeInterface Interface;
}
现在您可以从 Framwork 中的任何位置访问实例
static void Main(string[] args)
{
StaticClass.Interface = new SomeClass();
}
免责声明:我是下述库的作者。
不知道对你有没有帮助,我已经写了a library (very early version yet) that allows you to define static interfaces, by defining normal interfaces and decorating their methods with an attribute named [Static]
,例如:
public interface IYourInterface
{
[Static]
void DoTheThing();
}
(请注意,您没有明确地将此接口添加到您的实现中。)
定义接口后,您可以使用您选择的任何有效实现从代码中实例化它:
return typeof(YourImplementation).ToStaticContract<IYourInterface>();
如果在 YourImplementation
中找不到方法,则此调用在运行时失败并出现异常。
如果找到方法并且此调用成功,则客户端代码可以像这样以多态方式调用您的静态方法:
IYourInterface proxy = GetAnImplementation();
proxy.DoTheThing();