欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解PHP設(shè)計(jì)模式之依賴注入模式

 更新時(shí)間:2021年05月25日 15:46:08   作者:phpyu  
依賴注入模式:依賴注入是控制反轉(zhuǎn)的一種實(shí)現(xiàn)方式。要實(shí)現(xiàn)控制反轉(zhuǎn),通常的解決方案是將創(chuàng)建被調(diào)用者實(shí)例的工作交由 IoC 容器來完成,然后在調(diào)用者中注入被調(diào)用者(通過構(gòu)造器 / 方法注入實(shí)現(xiàn)),這樣我們就實(shí)現(xiàn)了調(diào)用者與被調(diào)用者的解耦,該過程被稱為依賴注入。

目的

實(shí)現(xiàn)了松耦合的軟件架構(gòu),可得到更好的測試,管理和擴(kuò)展的代碼

用法

DatabaseConfiguration 被注入 DatabaseConnection 并獲取所需的 $config 。如果沒有依賴注入模式, 配置將直接創(chuàng)建 DatabaseConnection 。這對測試和擴(kuò)展來說很不好。

例子

Doctrine2 ORM 使用依賴注入。 例如,注入到 Connection 對象的配置。 對于測試而言, 可以輕松的創(chuàng)建可擴(kuò)展的模擬數(shù)據(jù)并注入到 Connection 對象中。

Symfony 和 Zend Framework 2 已經(jīng)有了依賴注入的容器。他們通過配置的數(shù)組來創(chuàng)建對象,并在需要的地方注入 (在控制器中)。

UML 圖

代碼DatabaseConfiguration.php

<?php

namespace DesignPatterns\Structural\DependencyInjection;

class DatabaseConfiguration
{
    /**
     * @var string
     */
    private $host;

    /**
     * @var int
     */
    private $port;

    /**
     * @var string
     */
    private $username;

    /**
     * @var string
     */
    private $password;

    public function __construct(string $host, int $port, string $username, string $password)
    {
        $this->host = $host;
        $this->port = $port;
        $this->username = $username;
        $this->password = $password;
    }

    public function getHost(): string
    {
        return $this->host;
    }

    public function getPort(): int
    {
        return $this->port;
    }

    public function getUsername(): string
    {
        return $this->username;
    }

    public function getPassword(): string
    {
        return $this->password;
    }
}
?>

DatabaseConnection.php

<?php

namespace DesignPatterns\Structural\DependencyInjection;

class DatabaseConnection
{
    /**
     * @var DatabaseConfiguration
     */
    private $configuration;

    /**
     * @param DatabaseConfiguration $config
     */
    public function __construct(DatabaseConfiguration $config)
    {
        $this->configuration = $config;
    }

    public function getDsn(): string
    {
        // 這僅僅是演示,而不是一個(gè)真正的  DSN
        // 注意,這里只使用了注入的配置。 所以,
        // 這里是關(guān)鍵的分離關(guān)注點(diǎn)。

        return sprintf(
            '%s:%s@%s:%d',
            $this->configuration->getUsername(),
            $this->configuration->getPassword(),
            $this->configuration->getHost(),
            $this->configuration->getPort()
        );
    }
}
?>

測試Tests/DependencyInjectionTest.php

<?php

namespace DesignPatterns\Structural\DependencyInjection\Tests;

use DesignPatterns\Structural\DependencyInjection\DatabaseConfiguration;
use DesignPatterns\Structural\DependencyInjection\DatabaseConnection;
use PHPUnit\Framework\TestCase;

class DependencyInjectionTest extends TestCase
{
    public function testDependencyInjection()
    {
        $config = new DatabaseConfiguration('localhost', 3306, 'domnikl', '1234');
        $connection = new DatabaseConnection($config);

        $this->assertEquals('domnikl:1234@localhost:3306', $connection->getDsn());
    }
}
?>

以上就是詳解PHP設(shè)計(jì)模式之依賴注入模式的詳細(xì)內(nèi)容,更多關(guān)于PHP設(shè)計(jì)模式之依賴注入模式的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論