Service Locator Pattern Gamma Categorization: Behavioral Design Pattern Summary: Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates. Sample Code Problem: We want to get the service based on the interface. Code public interface IServiceLocator { T GetService < T >(); } public class ServiceLocator : IServiceLocator { private IDictionary < object , object > services ; public ServiceLocator () { services = new Dictionary < object , object >(); this . services . Add ( typeof ( IStorageService ), new CloudService ()); this . services . Add ( typeof ( INetworkService ), new WiFiService ()); } public T GetService < T >() { try { return ( T ) services [ typeof ( T )]; } ...
software engineering