Service locator pattern
   HOME

TheInfoList



OR:

The service locator pattern is a
design pattern A design pattern is the re-usable form of a solution to a design problem. The idea was introduced by the architect Christopher Alexander and has been adapted for various other disciplines, particularly software engineering. The " Gang of Four" ...
used in
software development Software development is the process of designing and Implementation, implementing a software solution to Computer user satisfaction, satisfy a User (computing), user. The process is more encompassing than Computer programming, programming, wri ...
to encapsulate the processes involved in obtaining a service with a strong
abstraction layer In computing, an abstraction layer or abstraction level is a way of hiding the working details of a subsystem. Examples of software models that use layers of abstraction include the OSI model for network protocols, OpenGL, and other graphics libra ...
. This pattern uses a central registry known as the "service locator", which on request returns the information necessary to perform a certain task. Proponents of the pattern say the approach simplifies component-based applications where all dependencies are cleanly listed at the beginning of the whole application design, consequently making traditional dependency injection a more complex way of connecting objects. Critics of the pattern argue that it is an
anti-pattern An anti-pattern in software engineering, project management, and business processes is a common response to a recurring problem that is usually ineffective and risks being highly counterproductive. The term, coined in 1995 by computer programmer An ...
which obscures dependencies and makes software harder to test.


Advantages

* The "service locator" can act as a simple run-time
linker Linker or linkers may refer to: Computing * Linker (computing), a computer program that takes one or more object files generated by a compiler or generated by an assembler and links them with libraries, generating an executable program or shar ...
. This allows code to be added at run-time without re-compiling the application, and in some cases without having to even restart it. * Applications can optimize themselves at run-time by selectively adding and removing items from the service locator. For example, an application can detect that it has a better library for reading JPG images available than the default one, and alter the registry accordingly. * Large sections of a library or application can be completely separated. The only link between them becomes the registry. * An application may use multiple structured service locators purposed for particular functionality/testing. Service locator does not mandate one single static class per process. * The solution may be simpler with service locator (vs. dependency injection) in applications with well-structured component/service design. In these cases, the disadvantages may actually be considered as an advantage (e.g., no need to supply various dependencies to every class and maintain dependency configurations). * Service location process can be made sensitive to a calling scope/context which can represent a specific business case. Instead of relying on a fixed DI which typically supplies an object with its dependencies via a constructor parameter passing, the service location call can be made in a business context as needed. For example, you may wish to invoke a "IShippingStrategy" instance obtained via a service location call, supplying a "ShippingContext" as a parameter to service locator stating what is being shipped from where and where to, so service locator can match the best case (e.g. using a pattern match score). This significantly simplifies complex business application architecture (e.g. medical scoring systems, routing) where circuitous decision making can be done via scoring of the best matching strategy located dynamically at a runtime


Disadvantages

* The registry hides the class' dependencies, causing run-time errors instead of compile-time errors when dependencies are missing (unlike when using constructor injection). * The registry makes code harder to
test Test(s), testing, or TEST may refer to: * Test (assessment), an educational assessment intended to measure the respondents' knowledge or other abilities Arts and entertainment * ''Test'' (2013 film), an American film * ''Test'' (2014 film) ...
, since all tests need to interact with the same global service locator class to set the fake dependencies of a class under test.


See also

*
Dependency injection In software engineering, dependency injection is a programming technique in which an object or function receives other objects or functions that it requires, as opposed to creating them internally. Dependency injection aims to separate the con ...
* Dependency inversion principle *
Java Naming and Directory Interface The Java Naming and Directory Interface (JNDI) is a Java API for a directory service that allows Java software clients to discover and look up data and resources (in the form of Java objects) via a name. Like all Java APIs that interface with host ...


References


External links


Sample code
Architectural pattern (computer science) Software design patterns {{Software-eng-stub