Design Patterns: Inversion of Control IoC, DI DI

basic concepts

Inversion of control (Inversion of Control, abbreviated as the IoC), one design principles of object oriented programming, can be used to reduce the degree of coupling between the computer code.

The most common method is called dependency injection (Dependency Injection, referred to as DI), there is a way called " dependent lookup " (Dependency Lookup).

Technical Description

Class A used in the object b Class B, under normal circumstances, the object code requires explicit A in B of a new.

After using DI techniques, the code A only need to define a private objects B, obtained without direct new object, but the object B to the new control program by the associated container out externally and injected into the class A references. DETAILED the acquired object is acquired when the state specified by the configuration file (e.g., XML).#

For example

The following code in a Database class, it need an adapter to interact with the database. We constructor instantiates the adapter, resulting in the coupling.

<?php
namespace Database;

class Database
{
    protected $adapter;

    public function __construct()
    {
        $this->adapter = new MySqlAdapter;
    }
}

class MysqlAdapter {}

The above code produces the problem:
1, if we want to change the adapter generation mode To use the new MySqlAdapter (String name) initialization adapter, you need to modify the code Database;
2, if you want to test a different impact on the Database objects MySqlAdapter very difficult, because the adapter the initialization is written in the dead of the constructor Database;
3, if the new MySqlAdapter () process is very slow, we hope to use single measure has been initialized good adapter Mock objects out of this process is very difficult.

Reconstruction using dependency injection:

<?php
namespace Database;

class Database
{
    protected $adapter;

    public function __construct(MySqlAdapter $adapter)
    {
        $this->adapter = $adapter;
    }
}

class MysqlAdapter {}

The above code, we adapter object is passed as a parameter to the constructor. Before calling the constructor has been initialized outside Database of good MysqlAdapter object. Like this non-initialized rely on their own initiative, and to pass through the external dependence of the way, we called dependency injection.

An upgraded version of reconstruction

<?php
namespace Database;

class Database
{
    protected $adapter;

    public function __construct(AdapterInterface $adapter)
    {
        $this->adapter = $adapter;
    }
}

interface AdapterInterface {}

class MysqlAdapter implements AdapterInterface {}

Database class now depends on the interface, compared to implementation-dependent have more advantages.

Suppose you work in a team, a colleague responsible for the design adapter. In the first example, we need to wait after completing adapter designed to unit test. Now that the dependence is an interface / convention, we can easily simulate interface testing, because we know that colleagues will realize that based on the agreement adapter

The benefits of a larger expansion of this method is the code becomes higher. If a year later we decided to migrate to a different database, we just need to write a corresponding adapter implements the interface and injected into account, since the adapter interfaces follow the convention, we do not need additional reconstruction.

Published 165 original articles · won praise 59 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_43972437/article/details/103858533