4.1. Service Locator
Service Locator is considered for some people an anti-pattern. It violates the Dependency Inversion principle.Service Locator hides class’ dependencies instead of exposing them as you would do using the Dependency Injection. In case of changes of those dependencies you risk to break the functionality of classes which are using them, making your system difficult to maintain.
To implement a loosely coupled architecture in order to get bettertestable, maintainable and extendable code. DI pattern and ServiceLocator pattern are an implementation of the Inverse of Control pattern.
4.1.2. Usage
- Zend Framework 2 uses Service Locator to create and share servicesused in the framework(i.e. EventManager, ModuleManager, all customuser services provided by modules, etc…)
4.1.4. UML Diagram
You can also find this code on GitHub
LogService.php
4.1.6. Test
Tests/ServiceLocatorTest.php