问题
I see that Composite pattern and dependency injection means
public function __construct(ClassToUseInterface $class) {
$this->class = $class
}
So, what's the difference ?
回答1:
The code as presented in your question neither represents dependency-injection, nor does it represent the composite pattern. Your code represents what is known as Dependency inversion. Let's answer your question :
- One way for your code to truly represent
Dependency injection
is to call theconstruct
function from code that is external to the class in which theconstruct
method is defined, passing it a concrete object that implementsClassToUseInterface
. The external code is said to inject the dependency and this is therefore known asDependency injection
. - The composite pattern (not to be confused with composition) on the other hand is a relationship such that a class represents both, an
IS-A
and aHAS-A
relationship at the same time with aclass
orinterface
that it extends from or implements. This allows a group of objects of a class to behave as if they represented a single object of the class.
Since I am not familiar with the php
syntax, here is a good example of the Composite pattern
in php
. In this example, the draw
method in the Container
class operates upon a group of Graphic
objects. To the caller, it's as if the draw
function was called on a single Graphic
object.
In case the linked example does not work, here is the code from the link (by Dario Ocles; 18 Jun 2007)
<?php
abstract class Graphic{
abstract public function draw();
}
class Triangle extends Graphic{
private $name = '';
public function __construct($name = 'unknown'){
$this->name = $name;
}
public function draw(){
echo '-I\'m a triangle '.$this->name.'.<br>';
}
}
class Container extends Graphic{
private $name = '';
private $container = array();
public function __construct($name = 'unknown'){
$this->name = $name;
}
public function draw(){
echo 'I\'m a container '.$this->name.'.<br>';
foreach($this->container as $graphic)
$graphic->draw();
}
public function add(Graphic $graphic){
$this->container[] = $graphic;
}
public function del(Graphic $graphic){
unset($this->container[$graphic]);
}
}
$tri1 = new Triangle('1');
$tri2 = new Triangle('2');
$tri3 = new Triangle('3');
$container1 = new Container('1');
$container2 = new Container('2');
$container3 = new Container('3');
$container1->add($tri1);
$container1->add($tri2);
$container2->add($tri3);
$container3->add($container1);
$container3->add($container2);
$container3->draw();
?>
In the linked/above example, the statement $container3->add($container1);
passes a Container
object to another object of Container
. This will again be termed as dependency injection
. The take away from this is that dependency injection
and composite pattern
don't mean the same thing. A composite
object can be passed as a dependency
. A composite
object is not the same as dependency injection
.
Disclaimer : All credit for the above program goes to it's original authors.
来源:https://stackoverflow.com/questions/30136414/composite-pattern-and-dependency-injection