The dependence inversion principle is dependent on abstraction, not specific. Simply put, we need to program the abstraction rather than the implementation, which reduces the coupling between the customer and the implementation module, the concepts such as IOC, Di, and IOC container are extended.
For process-oriented development, the upper layer calls the lower layer, and the upper layer depends on the lower layer. When the lower layer changes dramatically, the upper layer also needs to change, this will reduce the reusability of modules and greatly increase the development cost. Object-Oriented development solves this problem very well. In general, the probability of abstract changes is very small, so that the user program depends on abstraction, and the Implementation Details also depend on abstraction. Even if the implementation details are constantly changing, the customer program will not need to change as long as the abstraction remains unchanged. This greatly reduces the coupling between customer programs and implementation details.
Dependency inversion principle (DIP ):A Software Architecture Design Principle (abstract concept ).
Control reversal (IOC ):A Method for reversing streams, dependencies, and interfaces (specific implementation of DIP ).
Dependency injection (DI ):An Implementation Method of IOC used to reverse the dependency (the specific implementation method of IOC ).
IOC container:Dependency InjectionFrameworkTo map dependencies and manage object creation and lifecycle (di framework ).
The high-level module does not depend on the underlying module, but the underlying module depends on the interfaces of the high-level module (the high-level module defines the interface and the underlying module is responsible for implementation ).
High-level module (Interface): Abstraction underlying module (Implementation Interface): Implementation ==> the two should depend on abstraction, abstraction (high-level) independent implementation (underlying), implementation (underlying) dependent on abstraction (high-level ).
Here is an example:
1. If the dependency does not result in: the high-level module depends on the underlying module, that is, the underlying module becomes abstract. The high-level module must implement all the abstracted interfaces. Once a new module appears at the underlying layer, you need to modify the high-level module to break the open-closed principle.
2. If Dependency inversion occurs, the underlying module depends on the high-level module. That is to say, the high-level module becomes abstract. The underlying module only needs to implement high-level interfaces. Once a new module appears at the underlying layer, the high-level module does not need to be modified (the abstract interface definition remains unchanged ).
This shows the advantages of dip:
The system is more flexible:You can modify part of the code without affecting other modules.
More robust system:You can modify part of the code without causing a system crash.
More efficient system:Components are loosely coupled and reusable to improve development efficiency.
Next, let's talk about "IOC )":
DIP is a software design principle. It tells us what kind of relationship should be between modules. IOC is a specific software design pattern and tells us how to do it, in order to achieve decoupling between programs.
IOC (control inversion) provides abstraction between high and low-level modules, that is, third-party systems, that is, dependent objects (underlying objects) are not dependent on modules (high-level modules) directly create an object, but hand over the right to create the object to the third IOC container to create it, and then hand over the object to the dependent module (Lenovo just took the money, the ATM machine is a high-level module, it does not decide which bank card to insert itself, such as China Construction Bank or ABC. the right to decide which card to insert lies in US (a third party) and the card to insert, it gives us the services of any bank ).
There are three injection methods:
1. constructor injection: Injection Using constructor as its name implies
2. setter method injection: provides a setter method in the class to be injected.
3. Interface injection: Because of code intrusion, it is rarely used. The first two types are mostly used.
Therefore, the IOC container is born like this, which is a framework of Di to simplify our operations. IOC containers can dynamically create, inject objects, and manage object lifecycles, ing dependencies.
IOC containers include picocontainer, JBoss microcontainer, Soto, and spring.
Summary:
DIP is an idea of software design, while IOC is a software design model derived from dip.
Di is one of the specific implementation methods of IOC and is the most widely used.
The IOC container is a DI injection framework that manages the lifecycle and ing relationships of dependencies.
Http://www.cnblogs.com/lichenwei/p/3943960.html
Understanding of the dependency inversion principle (DIP) and IOC, Di, and IOC containers)