我正在做一个多平台的事情,但我对OOP并不是很好。目前我的代码是:
public interface IMessageBox {
void Show(string Text);
void Show(string Text, string Description, MessageBoxType Type);
MessageBoxResult ShowYesNo(string Text, string Description, MessageBoxType Type);
MessageBoxResult ShowYesNoCancel(string Text, string Description, MessageBoxType Type);
}
public class MessageBox : InstanceGenerator {
public static void Show(string Text) {
MessageBoxImpl.Show(Text);
}
public static void Show(string Text, string Description, MessageBoxType Type) {
MessageBoxImpl.Show(Text, Description, Type);
}
public static MessageBoxResult ShowYesNo(string Text, string Description, MessageBoxType Type) {
return MessageBoxImpl.ShowYesNo(Text, Description, Type);
}
public static MessageBoxResult ShowYesNoCancel(string Text, string Description, MessageBoxType Type) {
return MessageBoxImpl.ShowYesNoCancel(Text, Description, Type);
}
}
protected class InstanceGenerator {
public static IMessageBox MessageBoxImpl = null;
public static IWindow WindowImpl = null;
private static Assembly Instance = null;
private static string InstanceName = null;
private static Assembly LoadAssembly(string lib) {
string AppPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Assembly assembly = Assembly.LoadFile(Path.Combine(AppPath, lib + ".dll"));
return assembly;
}
private static object CreateInstance(string @class) {
Type type = Instance.GetType(InstanceName + "." + @class);
return Activator.CreateInstance(type);
}
private static object CreateInstanceFromPath(string lib, string @class) {
string AppPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Assembly assembly = Assembly.LoadFile(Path.Combine(AppPath, lib + ".dll"));
Type type = assembly.GetType(lib + "." + @class);
return Activator.CreateInstance(type);
}
/// <summary>
/// Inits the whole thing
/// </summary>
public static void Init() {
if (CurrentOS.IsWindows)
InstanceName = "Lib.Windows";
else if (CurrentOS.IsMac)
InstanceName = "Lib.MacOS";
else if (CurrentOS.IsLinux)
InstanceName = "Lib.Linux";
else // no implementation for other OSes
throw new Exception("No implementation of Lib for this OS");
Instance = LoadAssembly(InstanceName);
// initialize the classes
MessageBoxImpl = (IMessageBox) CreateInstance("MessageBox");
}
}
编辑:
InstanceGenerator返回从程序集加载的IMessageBox实例。有没有更好的方法来创建/连接到实例?实现所有相同的静态方法看起来并不是很好的解决方案。是否有更自动的方法将这些接口与类包装在一起,或者我做错了什么?
答案 0 :(得分:4)
您目前没有实现该界面。您不能使用 static 方法来实现接口。您必须使用实例方法。当然,您可以在实现中调用静态方法,但这是另一回事。
这听起来不像派生自 InstanceGenerator的MessageBox
- 我建议它应该撰写它:
public class MessageBox : IMessageBox {
private readonly InstanceGenerator generator;
public MessageBox(InstanceGenerator generator) {
this.generator = generator;
}
public static void Show(string Text) {
generator.MessageBoxImpl.Show(Text);
}
public static void Show(string text, string description, MessageBoxType type) {
generator.MessageBoxImpl.Show(text, description, type);
}
public static MessageBoxResult ShowYesNo(string text, string description,
MessageBoxType type) {
return generator.MessageBoxImpl.ShowYesNo(text, description, type);
}
public static MessageBoxResult ShowYesNoCancel(string text,
string description,
MessageBoxType type) {
return generator.MessageBoxImpl.ShowYesNoCancel(text, description, type);
}
}
我假设 MessageBoxImpl
尚未实施IMessageBox
,否则所有这一切都毫无意义......
答案 1 :(得分:1)
您的实现定义了一个实际上没有做任何事情的接口。接口的要点是允许您编写类似以下内容的代码:
instance_implementing_interface.DoSomething();
你没有那个。相反,你要定义一个接口,然后使用静态方法重写它所做的一切。接口的想法是,任何想要使用实例来完成其工作的人都可以使用实现该接口的任何实例,并相信它能够按照它所说的那样做(除非你习惯了用你自己的API骗你自己。)
事实上,在写完上述内容之后,我意识到我可能会认为你比实际上更困惑 - 经过几次重读之后,你似乎理解我所说的一切。这让我感到困惑,为什么你写了你的代码。你能解释为什么你故意在MessageBox
编写静态方法吗?