服務定位器模式
Service Locator 設計模式非常接近依賴注入。與 Bridge Pattern 一樣,此模式可用於從獨立於平臺的上下文中引用與平臺相關的程式碼。最有趣的是,這種模式依賴於單例模式 - 你放入服務定位器的所有內容都將是一個事實上的單例。
// Define a service locator class in your common project
public class ServiceLocator {
// A dictionary to map common interfaces to native implementations
private Dictionary<object, object> _services;
// A static instance of our locator (this guy is a singleton)
private static ServiceLocator _instance;
// A private constructor to enforce the singleton
private ServiceLocator() {
_services = new Dictionary<object, object>();
}
// A Singleton access method
public static ServiceLocator GetInstance() {
if(_instance == null) {
_instance = new ServiceLocator();
}
return _instance;
}
// A method for native projects to register their native implementations against the common interfaces
public static void Register(object type, object implementation) {
_services?.Add(type, implementation);
}
// A method to get the implementation for a given interface
public static T Resolve<T>() {
try {
return (T) _services[typeof(T)];
} catch {
throw new ApplicationException($"Failed to resolve type: {typeof(T).FullName}");
}
}
//For each native implementation, you must create an interface, and the native classes implementing that interface
public interface IA {
int DoAThing();
}
public interface IB {
bool IsMagnificent();
}
public class IosA : IA {
public int DoAThing() {
return 5;
}
}
public class DroidA : IA {
public int DoAThing() {
return 42;
}
}
// You get the idea...
// Then in your native initialization, you have to register your classes to their interfaces like so:
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.activity_main);
var locator = ServiceLocator.GetInstance();
locator.Register(typeof(IA), new DroidA());
locator.Register(typeof(IB), new DroidB());
}
}
public partial class AppDelegate : UIApplicationDelegate
{
UIWindow window;
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
window = new UIWindow(UIScreen.MainScreen.Bounds);
window.RootViewController = new UIViewController();
window.MakeKeyAndVisible();
var locator = ServiceLocator.GetInstance();
locator.Register(typeof(IA), new IosA());
locator.Register(typeof(IB), new IosB());
return true;
}
}
// Finally, to use your native implementations from non-native code, do as follows:
public void SomeMethodUsingNativeCodeFromNonNativeContext() {
// Some boring code here
// Grabbing our native implementations for the current platform
var locator = ServiceLocator.GetInstance();
IA myIA = locator.Resolve<IA>();
IB myIB = locator.Resolve<IB>();
// Method goes on to use our fancy native classes
}