ioc thought

  1. IoC theoretical background

We all know that in object-oriented software design methods, which are from the underlying implementation Ñ composed of objects, all objects through mutual cooperation, and ultimately the business logic of the system.
Here Insert Picture Description

Is similar to a mechanical watch cover will see similar to the above case, the respective gears are driving hour, minute and second hands clockwise rotation, thereby producing the correct time on the dial. Is described such a gear set of FIG. 1, it has a plurality of individual gears, the gears meshed with each other, work together to accomplish a common task. We can see that in such a gear set, a gear if there is a problem, it may affect the normal operation of the entire gear set,

The engagement between the gear set, coupling between the objects is very similar to the system software. Coupling between objects is inevitable and necessary, which is the basis for collaborative work . Now, along with the industrial application of increasingly large scale, dependencies between objects have become increasingly complex, often there will be multiple dependencies between objects , therefore, architects and designers for systems analysis and design, will face even greater challenges. Coupling system between objects is too high, the situation is indeed affect the body's inevitable.

Here Insert Picture Description

Coupling relationship will not only appear between the object and the object will appear between the modules of the software system, as well as between software and hardware systems. How to reduce between systems, coupling between modules and between the objects is one of the eternal pursuit of the goal of software engineering. In order to solve the coupling between objects too high a problem, Michael Mattson software experts proposed IOC theory, to achieve the "decoupling" between objects, the current theory has been successfully applied to practice, spring

2. What is Inversion of Control (IoC)
IOC is Inversion of Control acronym, most books translated as "inversion of control", and some books translated into "reverse-control" or "control upside down."
In 1996, Michael Mattson in the article about the object-oriented framework to explore a, first proposed the concept of IOC. For the basic idea of object-oriented design and programming, We have already talked a lot, and not repeat, in short, is to break down complex systems into a cooperative interaction of objects, these object classes after the adoption of the package, the internal implementation is transparent to external , thereby reducing the complexity of solving the problem, but also the flexibility to be reused and extended .IOC theory point of view raised roughly like this: by means of a "third party" to achieve, as shown below between objects that have dependencies:
Here Insert Picture Descriptionus come to a test: the IOC container between the figure above removed, and then look at the system:
Here Insert Picture Descriptionwe now see the picture that we want to realize the full contents of the entire system needs to be done. At this time, A, B, C, d between these four objects has no coupling relationship to each other without any contact , so, when you realize Afghanistan, and no need to go to consider the B, C and d, and dependencies between objects has been reduced to a minimum. So, as long as each member involved in the development of their own class can be achieved, without any relationship with others!

Let us look, Inversion of Control (IOC) in the end why play such a name we compare?

The system software is not introduced until the IOC container shown in Figure 1, the object A is dependent on the object B, then the object A to a point of time in the initialization operation or he must take the initiative to create an object using the object B has been created or B. whether you are creating or using an object B, control in their own hands.

Software system after the introduction of IOC container, this situation is completely changed due to the loss of a direct link between the IOC to join the vessel, the object A and the object B, so, when the object A need to run the object B, the IOC container will take the initiative create an object to place the object methyl acetate injection needed. By comparing before and after: A process of obtaining the object of dependent objects B, by the active behavior becomes a passive behavior, control over reversed, this is the "inversion of control" origin of the name.

IOC alias: dependency injection (DI)

In 2004, Martin Fowler discusses the same problem, since the IOC is Inversion of Control, so in the end is, after detailed analysis and demonstration, the "control what aspects of it were reversed?" He got the answer: "get the process is reversed dependent object with "after the control is reversed, the process of obtaining their dependent objects managed by the IOC container active injection so he" invert "take a more appropriate name is" dependency injection (dependency injection ). " His answer, in fact, shows how to achieve the IOC: the injection. The so-called dependency injection, that is, the IOC container during operation, dynamic dependencies injected into certain objects .

Therefore, dependency injection (DI) and reverse control (IOC) is the same thing from different angles described, by introducing the IOC container means is by way of injection dependency, decoupling between objects.

IOC brought us any good

We start with the example of USB, USB external devices using the built-in hard disk than, in the end bring any good?

  • First, USB device as an external host computer device, before inserting the host, and the host computer does not have any relationship, only after we are together, both before contact occurs, relevant. So, no matter what the problem either both appear, will not affect the operation of the other. This feature is embodied in software engineering, maintainability is good, very good for unit testing, debugging and easy to diagnose. Each Class code can be tested separately, independently of each other to each other, as long as their functions can correct, that is, low or no coupling between components coupled brings benefits.
  • Second, between USB devices and the host computer's independence, but also brings another benefit, the production of USB device manufacturers and manufacturers producing mainframe computer may be completely unrelated people, all doing things, among them the only need is to comply with the standard USB interface. Each development team members only need to be concerned to achieve their own business logic, do not need to care about other people who work in progress, because your job has nothing to do with others, you can test tasks alone, you do not have to depend on the task other people's assembly, no longer have to pull clear responsibility. So, in a medium-sized project, team members clear division of responsibility clear , it is easy to be a big task into small tasks, development efficiency and product quality will be greatly enhanced.
  • Third, with a USB external devices can be plugged into any USB-enabled device, you can plug into the host computer, the machine can be plugged into DV, USB external device may be used repeatedly . In software engineering, this feature is a good reusability, we can have common components universal independent, repeatedly used to other parts of the project or other projects
  • Fourth, with the external USB device, the module has a hot swappable. IOC generates object method to the external mode, that is, the resulting object is defined in the configuration file, so that when we replace an implementation subclass will become very simple, just modify the configuration files on it, complete with Hot Swap feature.

The principle

We can work mode IOC containers seen as the factory model of sublimation + Singleton, as an IOC container factory, this factory to produce the object definitions given in the configuration file, and then use the programming language reflection programming, to form the corresponding object class name given in the configuration file. From the implementation point of view, IOC is put before the factory method to write the dead object code generation, by changing the configuration file to define, that is, the plant generates both independent and objects separated by, the purpose is to improve the flexibility and maintainability. While a global shared objects

Published 23 original articles · won praise 2 · Views 928

Guess you like

Origin blog.csdn.net/metjoyful/article/details/103394148