A good memory is better than a bad pen. 87-spring3 Study (8)-ioc understanding and metaphor

Source: Internet
Author: User

学习spring,不可避免就要学习IoC和DI(控制反转和依赖注入)。这些名词总是那么拗口。

First of all we need to understand what ioc/di do, or else people ask,
"Old fan, fennel bean" anise "word has that three kinds of writing, you know? ”
"Old van, the IOC name has several sayings, you know?" ”
"Old van, what is the IOC for, you know?" ”
......

IoC (inversion of control, controlled inversion), DI (Dependency injection, Dependency injection)
In fact, the role of the Ioc/di two words-decoupling, said 1000 10,000, or inseparable from the two words. So, the role of Ioc/di is decoupling. The tightly coupled logic eventually becomes a loosely coupled structure.

The so-called IOC, for the spring framework, is the responsibility of spring to control the object's life cycle and the relationship between objects.
A classic metaphor for the IOC

For a simple example, how do we find a girlfriend? The common situation is that we go everywhere to see where there is a beautiful body and good mm, and then inquire about their interests, QQ number, telephone number 、........., find ways to know them, give it to send their want, and then hey ... The process is complex and profound, and we have to design and face each link ourselves.
The same is true of traditional program development, in an object, if you want to use another object, you have to get it (your own new one, or a query from Jndi), after the use of the object will be destroyed (such as connection, etc.), the object will always and other interfaces or classes together.

So how does the IOC do it? It's kind of like finding a girlfriend through a dating agency, introducing a third party between me and my girlfriend: the Marriage Institute. Matchmaking management of a lot of men and women's information, I can give a list of matchmaking, tell it I want to find a girlfriend, such as like Michelle Reis, figure like Lin Xire, singing like Jay Chou, speed like Carlos, technology like Zidane, and then the matchmaking will be according to our requirements, provide a mm, We just have to go to love her and get married. As simple as it is, if a matchmaking person doesn't meet our requirements, we'll throw an exception. The whole process is no longer controlled by myself, but by a similar container-like institution that has a matchmaking system.

The metaphor of personal perception is very good, but some of the details are not perfect, but the destruction of this paragraph, feel a little far-fetched, I decided to write a metaphor.

A parable about the IOC by a classmate of the rice
A classmate wants to buy stock? The common situation is that a classmate first to know that there is a stock this thing, and then to understand the corresponding companies in the stock, these companies are doing what, the business situation, how much gross margin, how much net assets ..., and then in a lot of stocks, choose their own right, we have to check, analyze, Check each link.
Traditional program development is similar, we first consider what kind of function we do, what we need to use the technology, we have to go to a lot of technology to pick a suitable, if we need to use an object, I have to get it (buy a stock), if this object is long-term existence, So a classmate will keep this object for a long time (long-term holdings), if the object is short-term, then the classmate will destroy this object (sell this stock).
And the IOC is like the fund, if the students feel that China's "high-end manufacturing" has a future, then do not have to analyze a large number of stocks, as long as the purchase of the corresponding "high-end manufacturing" fund; If the students feel the blue sky and water is the Chinese dream, then directly buy "clean energy" fund can be.
If the fund does not provide us with the right kind, we will throw an exception (we will not buy funds).
After the purchase, the whole process is no longer controlled by me, but by a similar container-like institution in the fund company.

A good memory is better than a bad pen. 87-spring3 Study (8)-ioc understanding and metaphor

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.