问题
I've been programming for some time but never got interested in knowing in theory what each concept means, I may be using a variety of programming concepts, but without knowing it.
Service Locator: For me, refers to a record of shortcuts to speed up development by reducing the amount of code. One question is: may Locator refer to namespaces/classes only, or I can have a registry of variables?
Here is my understanding of it:
$locator = new ServiceLocator()
$locator->set('app', new System\Application());
$locator->set('db', new System\Pdo());
// Get the objects
$locator->get('db')->connect();
$locator->get('app')->run();
Dependency Injection (and Dependency Injection Container): Injecting objects within objects, allowing faster access to these regardless of the factory pattern. And DI Container?
Here is my understanding of it:
$app = new System\Application(System\Config::load());
Inversion of Control: Don't understand this Design Pattern (or understand but don't know if what I do is IoC)
Then, in theory (preferably with simple examples), what does each of these concepts mean? Am I correct, or what is wrong / can be improved?
Thanks!
回答1:
I think you understood correctly the Service Locator.
About Dependency Injection, means that if an object has constructor and/or property dependencies, these are injected in the object by outside, as opposed to the object getting the dependencies by itself
public class MyClass
{
private $_dep;
public function __construct($dep=null)
{
//$dep has to be injected
$this->_dep=$dep;
}
//this is wrong because it couples MyClass to a specific Dependency implementation
public function __construct()
{
$this->_dep=new Dependency();
}
}
$dep=new Dependency();
$obj=new MyClass($dep);
Usually the constructor takes an abstraction (an interface) as the param, and outside the class is instantiated a concrete implementation which then is passed on to the constructor when creating a new instance of MyClass.
A DI Container, handles the dependency injection automatically. You just configure it, so that it knows what concrete classes to be returned when an abstraction is asked. The Container handles object creation, injecting dependencies via constructor and/or properties. Depending on the container (I don't know an example for php, I'm familiar only with .net DI Containers) you might have to register also the types of objects that can be created by it.
Inversion of Control means that instead of a higher level class depending on a lower level class (dependency) implementation, the control is inversed so the lower level class implementation depends on an abstraction required by the higher level class.
//abstraction defined for the use of higher level class
public interface IRepository {}
// that's the dependency, the lower level class
public class XmlRepository implements IRepository {}
//the higher level class
public class MyClass
{
public function __construct(IRepository $repo) {}
}
IoC and DiC go together as the DI Container offers the IoC feature.
回答2:
Service Location and Dependency Injection is at first for decoupling classes so that they can be easily tested and changed.
When you compare the register and resolve parts of an IoC Container with a Service Locator it seems to be the same.
You can use an IoC Container as a Service Locator, which is considered to be an anti pattern. When you use Service Location you always have to call the Service Locator actively all over your architecture. So you decouple your classes, but on the other hand you couple them all to the Service Locator. Furthermore dependency discovery is more difficult with a Service Locator, because you are hiding dependencies. Whereas with Dependency Injection you make the dependencies "public" by using Constructor Injection.
When you use an IoC Container you use Dependency Injection (Constructor Injection or Property Injection). The IoC Container is now able to resovle the dependency graph by looking at the constructor parameters and create the whole dependency graph. This is called auto-wiring. A Service Locator is not able to auto-wiring dependencies. As I already mentioned, you are not forced to use auto-wiring, you could easily use the IoC container like a Service Locator by simple calling the IoC Container in each class directly, BUT YOU SHOULD NOT!
See also: https://stackoverflow.com/a/11319026/175399
来源:https://stackoverflow.com/questions/10049940/service-locator-dependency-injection-and-container-and-inversion-of-control