Two metaphor for dependency Injection

Source: Internet
Author: User
Tags ip number
What is IOC = inversion of control and dependency injection )? I have never understood it. Today I see two metaphors and I think they are more vivid.
IOC, in the vernacular, is the relationship between control programs by containers, rather than in non-traditional implementations, directly controlled by program code. This is the concept of "control reversal": control is transferred from application code to external containers, and control is transferred.
When the industry is arguing over IOC, the master Martin Fowler also spoke up and named IOC as "inversion of control containers and the dependency injection pattern, IOC has a new name: "dependency injection )". Compared with IOC, "dependency injection" is indeed a more accurate description of this ancient and fashionable design concept. According to the name, dependency injection is the dependency between components, which is determined by the runtime of the container. Specifically, the container dynamically injects a dependency into the component.
I:
In the above example, the laptop is connected to the peripheral storage device through a pre-specified interface (USB). For the notebook, only the user-specified data is sent to the USB interface, where the data goes is determined by the USB device currently connected. Before the USB device is loaded, it is impossible for the notebook to predict which device the user will connect to on the USB interface. Only after the USB device is connected will the dependency between the devices become.
Corresponding to the above description about the dependency injection mechanism, at runtime (system boot, USB device loading) by the container (Windows operating system running in the notebook) inject dependencies (laptops rely on USB devices for data access) into components (Windows File Access Components ). This is a version of the dependency injection mode in the real world.
Many beginners often fall into the "dependency injection" field. How can this problem be solved? . This example can help you briefly understand the meaning. The goal of dependency injection is not to bring more power to the software system, but to improve the probability of Component Reuse and build a flexible and scalable platform for the system. Compare the USB interface with the previous serial/parallel interface and PS2 interface, so we can understand what it means.
II:
First, I want to talk about IOC (inversion of control, control reversal ). This is the core of spring. For the Spring framework, the so-called IOC is that spring is responsible for controlling the relationship between object lifecycles and objects. What does this mean? For example, how do we find a girlfriend? The common situation is that we go around to see where there is a pretty and good mm, and then inquire about their interests, such as love, QQ, phone number, IP number, IQ number ........., Find a way to get to know them and give them what they want ...... This process is complex and profound, and we must design and face each link on our own. This is also true for program development of data transfer. To use another object in an object, you must obtain it (New by yourself or query one from JNDI ), after use, the object will be destroyed (such as connection), and the object will always be combined with other interfaces or classes.
How does IOC work? It's a bit like finding a girlfriend through a matchmaking agency. I introduced a third party between my girlfriend and me: the marriage agency. The matchmaking agency manages a lot of information about men and women. I can submit a list to the matchmaking agency to tell it what kind of girlfriend I want to find, such as Li jiaxin, Lin xilei, and Jay Chou, the speed is like Carlos, the technology is like Zidane, and then the matchmaking agency will provide a mm according to our requirements. We just need to fall in love with her and get married. It is simple and clear. If the person selected by the matchmaking agency does not meet the requirements, we will throw an exception. The entire process is no longer controlled by myself, but controlled by a mechanism similar to a container such as matchmaking agency. This is the development method advocated by spring. All classes will be registered in the spring container to tell Spring what you are and what you need, then spring will take the initiative to give you what you want when the system runs properly, and also give you something else you need. The creation and destruction of all classes are controlled by spring. That is to say, the object lifecycle is not referenced but spring. For a specific object, it used to control other objects. Now all the objects are controlled by spring, so this is called control inversion. If you still don't understand, I decided to give up. # C # column

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.