(Rpm) Ioc, inversion control and dependency injection

Address reprint: https://zhuanlan.zhihu.com/p/95869440

 

Inversion control
inversion of control (Inversion of Control, referred to as the IoC), is a design of object-oriented programming, its role is to reduce the degree of coupling between the code (low [bulk] coupling). IoC its two main methods to achieve a dependency injection (Denpendency Injection), one is dependent lookup (Denpendency Lookup). IoC dependency injection by a method to solve the problem of a high degree of coupling between the code.

How to understand inversion of control
under control before understanding reversed, we may wish to split, split into the inversion of control "control" and "reverse." Thus, we can draw the following three questions:
Who is who controls?
What is the control?
What is the reverse?
To get to know the why, we need to find a starting point, then the proposed inversion of control What is the purpose? Our purpose is to reduce it in front of the coupling between the code.
So what is the degree of coupling it in a real project, inevitably there will be more than one object, there is bound to depend on the relationship between the object and that the object is, the lower this dependency relationship between objects, it shows the degree of coupling between objects the lower, higher scalability that code. After understanding the degree of coupling, if things become clearer, and the next we first sort out the purpose of inversion of control is used to reduce the coupling between code, dependent relationship between the degree of coupling with the object code, but also this means that the dependencies between objects we want to control the lower the better.

Here, we use an example to explain, for example, we now have two objects A and B, and now you want to use in the A object B, how we do it, is not new to a B object A object ah, that is, a control said object B object, now we are not to understand, control refers to the creation of dependent objects. In this process, who is the boss, of course, it is A, B, A dominates creation, so A final say.

Control refers to the current application (A subject) dominated all resources (B subject) acquisition, which is controlled dependent on resource acquisition process!

At this point, we have to get to know two, there is a reversal. We know from the previous analysis, the object is dependent on the current object of control, then the reverse, that is not dependent on the current object in the control object. Those who control it, it is then we have to explain the dependency injection, three under before we first summarize our previously proposed why.

Who controls who: current application dependent control resource (object data, etc.);
what controls: a Resource acquisition process;
what reverse: obtaining resource-dependent procedure is reversed;

dependency injection (Denpendency Injection, referred to as the DI):
before we analyze, the control is reversed, and that is dependent on resources acquisition process is reversed. Before the initiative to obtain by the application becomes a passive acceptance. Also in the prior understanding of dependency injection, we are also under way to split accordance with the above, namely:
Who dependent?
What relies?
Who is injected?
What is the injection?
Just now we have to analyze the application by the change from passive to active, which means that the application relies on IoC container, to help it indirectly dependent objects acquired resources. Then the dependent objects of resources injected into the application, with the front of the bedding, we can complete the logical answer to the above question.
Who dependence: application relies IoC container;
what dependencies are: the dependent objects of resources;
who was injected with: external resource dependencies;
what injection are: application-dependent resources are injected;

Summary:
Dependency Injection is One way to achieve control reversal, that inversion of control is dependent on carrier injection, which is the container; prior to the application to complete the creation of the dependent objects, and now all done by the IoC container. Dependencies between its value is the stripping assembly implemented to achieve high cohesion component, scalability and high availability.

Guess you like

Origin www.cnblogs.com/zhaobao1830/p/12004954.html