IoC and DI (Inversion of Control and the Dependency Injection) Analytical popular

Transfer: https: //cngolon.iteye.com/blog/2187021

IoC - Inversion of Control invert
 DI - Dependency Injection dependency injection


        To understand the two concepts above, it is necessary to find out the following issues:

  • Who are the participants?
  • Dependence: Who depends on whom? Why rely on? 
  • Injection: Who is injected to whom? What in the end the injection?
  • Inversion of Control: Who controls? What control? Why it is called inversion (reversal should have a positive turn)?
  • Dependency Injection and Inversion of Control is the same concept right?

        Here's a brief answer the above questions, these questions figured it out, IoC / DI will understand.
(1) Participants who have:

        Participants generally have three parties, one is an object; one IoC / DI container; the other is an object of external resources.
        But also to explain the term, refers to an object is arbitrary, ordinary Java objects; IoC / DI container simple point that is used to refer to implement a framework program IoC / DI function; external resource object refers to an object needed, but is acquired from an external object, are collectively referred to resources, such as: object requires other objects, or objects need to file resources and so on.


(2) who depend on who:

        Of course, an object is dependent on the IoC / DI container


(3) Why rely on:

        Objects need IoC / DI container objects to provide the required external resources


(4) Who is injected to whom:

        It is clear that IoC / DI container inject an object


(5) what in the end is injected:

        It is injected into the external resources needed for an object


(6) Who controls who:

        Course IoC / DI container to the controlled object


(7) What control:

        The main control is to create an object instance


(8) Why is called reverse:

        Reverse is relative to the positive terms, then what regarded it forward? Consider an application in the conventional case, if you want to use C in which A, how would you do it? Of course, go directly to create objects of C, that is, in the A category in the initiative to obtain the required external resources C, this is called positive. So what is it the reverse? Class A is no longer actively to acquire C, but passively wait, wait IoC / DI obtain an instance of a container C, and then injected into a reversed category A.
 

        Legend used to explain, when there is no look IoC / DI, the use of a conventional class A schematic class C, shown in Figure 7:



                                      A schematic diagram of a conventional used in FIG. 7 C

When there IoC / DI container, A Class C to create a longer active, shown in Figure 8:



                                     FIG 8 A is no longer actively created Class C


Instead passively wait, wait IoC / DI obtain an instance of a container C, and then injected into a reverse class A, as shown in Figure 9:


                                               Figure 9 a schematic diagram of the program structure IoC / DI container


(9) dependency injection and inversion of control is the same concept it?


        According to the above teachings should be able to see it, dependency injection and inversion of control is a different description of the same thing, from a certain aspect, that is, they describe different angles. Dependency injection is from the application point in the description, it can be described in complete dependency injection point: application relies container create and inject external resources it needs; the inversion of control from the perspective of the container in the description, describe a complete point: container control applications, the reverse injection of external resources to the application required by the container application.


(10) Summary about:

        In fact, IoC / DI programming bring the biggest change is not from the code, but in terms of ideology, change "master-slave transposition" occurred. The application was originally boss, to get what resources are to take the initiative, but in IoC / DI thought, the application becomes passive, and passive waiting for IoC / DI container to create and inject the resources it needs.
        So a small change actually is a big step forward programming ideas, thus effectively separates objects and external resources it needs so that they are loosely coupled, help function multiplexing, more importantly, makes the whole system program structure becomes very flexible.

Guess you like

Origin www.cnblogs.com/lucm/p/11132241.html