Composite pattern and Dependency Injection

岁酱吖の 提交于 2019-12-24 03:25:44

问题


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 :

  1. One way for your code to truly represent Dependency injection is to call the construct function from code that is external to the class in which the construct method is defined, passing it a concrete object that implements ClassToUseInterface . The external code is said to inject the dependency and this is therefore known as Dependency injection.
  2. 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 a HAS-A relationship at the same time with a class or interface 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!