Singleton Vs ServiceLocator
问题 What are the advantages and disadvantages of using a Service Locator versus a singleton? I've read that singletons are bad but I'm wondering if s Service Locator would be generally a better way of doing things. 回答1: Both approaches are bad in that it's not obvious from class contract what are its' dependencies. That is, private void foo() { var x = SomeSingleton.Instance.GetX(); var y = ServiceLocator.GetService<IProvider>().GetY(); } has references to SomeSingleton and IProvider buried deep