PHP設計模式之依賴注入模式(Dependency Injection)代碼實例大全(15)

目的

實現了鬆耦合的軟件架構,可獲得更好的測試,管理和擴展的代碼php

依賴注入模式:依賴注入(Dependency Injection)是控制反轉(Inversion of Control)的一種實現方式。要實現控制反轉,一般的解決方案是將建立被調用者實例的工做交由 IoC 容器來完成,而後在調用者中注入被調用者(經過構造器 / 方法注入實現),這樣咱們就實現了調用者與被調用者的解耦,該過程被稱爲依賴注入。laravel

用法

DatabaseConfiguration 被注入 DatabaseConnection 並獲取所需的 $config 。若是沒有依賴注入模式, 配置將直接建立 DatabaseConnection 。這對測試和擴展來講很很差。面試

例子

  • Doctrine2 ORM 使用依賴注入。 例如,注入到 Connection 對象的配置。 對於測試而言, 能夠輕鬆的建立可擴展的模擬數據並注入到 Connection 對象中。sql

  • Symfony 和 Zend Framework 2 已經有了依賴注入的容器。他們經過配置的數組來建立對象,並在須要的地方注入 (在控制器中)。shell

UML 圖

★官方PHP高級學習交流社羣「點擊」管理整理了一些資料,BAT等一線大廠進階知識體系備好(相關學習資料以及筆面試題)以及不限於:分佈式架構、高可擴展、高性能、高併發、服務器性能調優、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql優化、shell腳本、Docker、微服務、Nginx等多個知識點高級進階乾貨設計模式

代碼

  • 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
    {
        // 這僅僅是演示,而不是一個真正的  DSN
        // 注意,這裏只使用了注入的配置。 因此,
        // 這裏是關鍵的分離關注點。

        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 互聯網架構師成長之路*「設計模式」終極指南數組

PHP 互聯網架構師 50K 成長指南+行業問題解決總綱(持續更新)服務器

面試10家公司,收穫9個offer,2020年PHP 面試問題架構

★若是喜歡個人文章,想與更多資深開發者一塊兒交流學習的話,獲取更多大廠面試相關技術諮詢和指導,歡迎加入咱們的羣啊,暗號:phpzh(君羊號碼856460874)。併發

2020年最新PHP進階教程,全系列!

內容不錯的話但願你們支持鼓勵下點個贊/喜歡,歡迎一塊兒來交流;另外若是有什麼問題 建議 想看的內容能夠在評論提出

相關文章
相關標籤/搜索