I’m doing a multiplatform thing, but I’m not very good with OOP. Currently my code is:
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");
}
}
EDIT:
Where InstanceGenerator returns an instance of the IMessageBox loaded from an assembly. Is there a better way to create/connect to an instance? Implementing all the same static methods looks not good soltution at all. Is there a more automatic way to wrap those interfaces with a class or I’m doing something wrong?
You’re not currently implementing the interface. You can’t use static methods to implement an interface. You have to use instance methods. You can call static methods within the implementation, of course, but that’s a different matter.
It doesn’t sound like
MessageBoxshould derive from InstanceGenerator – I’d suggest that it should compose it:I’m assuming that
MessageBoxImpldoesn’t already implementIMessageBoxas otherwise all of this is pretty pointless…