Design Architecture IOC and dependency inversion injection control (a)

Ali P7 mobile Internet architect, Advanced Video (Daily update) for free learning, please click: https://space.bilibili.com/474380680

1. What is the IoC

Ioc-Inversion of Control, that is, "inversion of control", not a technical, but a design idea. In Java development, Ioc means that you designed objects to the container control, rather than the traditional direct control over your internal object. Ioc understand how good it? Ioc good key to understanding is to define "who is who controls what control, why is reversed (reversal should have a positive turn), which reversed the terms," ​​that we have to analyze in depth:

Who controls who and what control: traditional Java SE programming, we directly create objects through new inside the object, a program initiative to create dependent objects; and IoC is a special container to create these objects, which come from the Ioc container create a control object; who controls? Of course IoC container control object; what control? That is the main control external access to resources (including not just objects such as files, etc.).

Why is reversed, what reversed: there have reverse forward, legacy applications by our own initiative in an object to acquire control directly dependent objects, that is, forward; while the reverse is from the container to help create and inject dependent objects; why is reversed? Because the container to help us find and inject the dependent objects, the object of accepting it passively dependent objects, it is the reverse; what reversed? The acquisition is dependent on the object reversed.

With a legend to explain that traditional programming shown in Figure 2-1, the initiative to create the relevant objects are then combined:

 
19956127-dcf6b236d5fd1de9.JPG
Traditional application schematic

When there IoC / DI container, no longer active in the client to create a class of objects, shown in Figure 2-2:

 
19956127-a7d7ed161a50ee33.JPG
After the program has a structural diagram IoC / DI container

2. What do IoC

IoC is not a technology, but an idea, an important rule of object-oriented programming that guides how we design a loosely coupled, better program. Traditional applications are created by our initiative within the class dependent objects, resulting in a high coupling between classes, it is difficult tests; Once you have IoC container to create and find dependent objects of control to the container by container implanting a combination of objects, between objects and the objects are loosely coupled, which would also facilitate the testing, which will help multiplexing function, more importantly, makes the whole architecture of the program has become very flexible.

In fact, IoC programming bring the biggest change is not from the code, but ideologically, "master-slave transposition" of change. 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 IoC container to create and inject the resources it needs.

IoC good indication of one of the objects oriented design rules - Hollywood rule: "Do not come to us, we are looking for you"; that is, to help find the appropriate objects by the IoC container object and dependency injection, rather than take the initiative to find the object.

3, IoC 和 IN

DI-Dependency Injection, namely "dependency injection": is the dependencies between components at runtime is determined by the vessel, said the image that will be injected by the container dynamic dependencies into a component. Dependency injection is not intended to bring more functionality to software systems, but to enhance component reuse frequencies, and for the system to build a flexible, scalable platform. Via dependency injection mechanism, we need only a simple configuration, without any code can specify the target resources needed to complete their business logic without having to be concerned about the specific resources come from, who to achieve.

DI is the key to understanding: "Who dependence, why rely on, who is who inject, inject something" that we have to analyze in depth:

Who depends on whom: of course, is application dependent on the IoC container;

Why rely on: the application needs IoC container to provide external resources required objects;

Who injection: it is clear that the object is injected into the IoC container application to an object, the application-dependent;

● injected what: that is injected into the external resources needed for an object (including objects, resources, constant data).

IoC and DI by what to do with it? In fact, they are the same concept describing a different point of view, due to the inversion of control concepts vague (probably only be understood as a container control object this level, it is hard to think of going to protect object-relational), so in 2004 the master at Martin Fowler has given a new name: "Dependency injection", relatively speaking IoC, "dependency injection ****" **** explicitly described the "object dependency IoC **** is injected into the container configuration dependent objects."

Note: If you want more in-depth understanding of IoC and DI, please refer to the masters Martin Fowler's a classic article "Inversion of Control Containers and the Dependency Injection pattern", the original address: http://www.martinfowler.com/ Articles / injection.html .

Original link https://www.iteye.com/blog/jinnianshilongnian-1413846
Ali P7 mobile Internet architect, Advanced Video (Daily update) for free learning, please click: https://space.bilibili.com/474380680

Guess you like

Origin www.cnblogs.com/Android-Alvin/p/12103014.html