Skip to content

Latest commit

 

History

History
55 lines (45 loc) · 1.27 KB

dependency_inversion.md

File metadata and controls

55 lines (45 loc) · 1.27 KB

Dependency inversion principle

DEPEND ON ABSTRACTIONS NOT ON CONCRETIONS

High-level modules should not depend on low-level modules. Both should depend on abstractions Abstractions should not depend on details. Details should depend on abstractions

Real world example

Code

Without DIP

class PasswordReminder
        {
            protected $dbConnection;

            // this is breaking the dependency inversion principle
            // because PasswordReminder should not know the type of the connection
            // HIGH-LEVEL MODULES SHOULD NOT DEPEND ON LOW-LEVEL MODULES
            public function __construct(MySQLConnection $dbConnection)
            {
                $this->dbConnection = $dbConnection;
            }
        }

With DIP

interface ConnectionInterface
{
    public function connect();
}

class DbConnection implements ConnectionInterface
{
    // now the low-level module depends upon an the abstraction
    public function connect()
    {
        // connect to db
    }
}

// high-level module depends upon an abstraction
class PasswordReminder
{
    protected $dbConnection;

    public function __construct(ConnectionInterface $dbConnection)
    {
        $this->dbConnection = $dbConnection;
    }
}