Primary knowledge control inversion (IoC)

Source: Internet
Author: User

First, the definition

Control inversion (inversion of controls, abbreviated to IOC) is a design principle in object-oriented programming that can be used to reduce the degree of coupling between computer code.

by controlling the inversion, the object is created by an external entity that regulates all objects within the system, passing a reference to the object on which it depends. It can also be said that the dependency is injected into the object.

Second, the principle

IoC, intuitively speaking, is the relationship between the container control program, rather than the traditional implementation, directly manipulated by the program code. This also

It is the concept of the so-called "inversion of Control": control is transferred from the application code to the external container, and the transfer of control is called reversal.

The IOC embodies the Hollywood principle that "Don t call me, I'll call you."

Third, technical description

Class A uses object B of Class B, and in general, you need to explicitly type a B object in the code of a.

with the dependency injection technique,The code of a only needs to define a private B object, does not need a direct new to obtain the object, but through the relevant container control program to the external new and inject the B object into the Class A reference. The state specified by the configuration file (such as XML) is used when the method is obtained and the object is fetched.

Iv. main ways of realization

1, Dependency Injection (Dependency injection)

components do not do location queries, only provide common Java methods to let the container to determine the dependency relationship.

2,Dependency lookup (Dependency lookup)

Dependency Lookup is more proactive, when needed, by invoking the method provided by the framework to obtain the object, the acquisition needs to provide the relevant configuration file path,key and other information to determine the state of the acquisition object . The container provides the callback interface and context to the component. both EJB and Apache Avalon Use this approach.

The difference between the two is:

The former is a passive receiving object , which creates a dependent B object during instance creation of Class A, which is judged by type or name to inject different objects into different properties, while the latter is an object that actively requests the response name. The time to obtain a dependent object can also be freely controlled in the code.

V. Advantages and Disadvantages

1.What are the IOC's greatest benefits?

Because the object generation is defined in XML, when we need to change the implementation subclass it will be very simple (generally such objects are implemented in some kind of interface), as long as the XML can be modified, This allows us to even get hot-swappable objects (a bit like USB ports and SCSI hard drives).

2. What is the IOC's biggest drawback?

1 , the steps to build an object become complicated (in fact, it's quite simple), and for those who are not accustomed to this way, they feel awkward and not intuitive.

2 , object generation is a loss of efficiency because it is programmed with reflection. But this loss is negligible relative to the increased maintainability and flexibility of the IOC, unless the generation of an object is particularly demanding in terms of efficiency.

3 , The lack of support for IDE refactoring operations, if you want to rename the class in Eclipse, you also need to go to the XML file manually to change, which seems to be the shortcoming of all the XML way.

Vi. concluding remarks

This blog is just a simple understanding of dependency reversal, the next blog, will be based on their previous learning design patterns, the dependency reversal and design patterns to a pot stew, to see what the dependency reversal is what? Please look forward to it!


Primary knowledge control inversion (IoC)

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.