复合模式和依赖注入 [英] Composite pattern and Dependency Injection

查看:70
本文介绍了复合模式和依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到复合模式和依赖注入意味着

I see that Composite pattern and dependency injection means

public function __construct(ClassToUseInterface $class) {
$this->class = $class
}

那么,有什么区别?

So, what's the difference ?

推荐答案

问题中显示的代码既不表示依赖项注入,也不表示复合模式。您的代码代表了依赖项倒置。让我们回答您的问题:

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. 一种代码真正代表依赖注入的方法是从定义了 construct 方法的类外部的代码中调用 construct 函数,并将其传递给一个实现 ClassToUseInterface 的具体对象。据说外部代码注入依赖项,因此被称为依赖注入

  2. 另一方面,复合模式(不要与组成混淆)是这样的关系,即类同时表示 IS-A HAS -A 与同时从其扩展的接口的关系实施。这允许类的一组对象的行为就像它们代表类的单个对象一样。

  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.

由于我不熟悉 php 语法,因此此处是一个很好的例子 php 中的复合模式。在此示例中,容器类中的 draw 方法对一组 Graphic 对象。对于调用者来说,就好像在单个 Graphic 对象上调用了 draw 函数。

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.

如果链接的示例不起作用,以下是链接中的代码(作者Dario Ocles; 2007年6月18日)

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();
?>

在链接/以上示例中,语句 $ container3-> add ($ container1); Container 对象传递给 Container 的另一个对象。这又称为依赖注入。这样做的好处是依赖注入复合模式并不意味着同一件事。可以将复合对象作为依赖项进行传递。 复合对象与依赖注入不同。

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.

这篇关于复合模式和依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆