What exactly is IOC and di?

Source: Internet
Author: User

When we learn the spring framework, we can always see the word IOC, and we often hear the word di, so what do they mean by each other? Next, let's talk about the individual's understanding of these two concepts.

I. IOC and DI Concepts

IOC (Control inversion): all called: Inverse of control. The literal understanding is that the inversion of control reverses the control of a built-in object in its own object, which is no longer controlled by its own object, but is created by a third-party system that controls the creation of the built-in object.

  DI (Dependency injection): all called dependency injection, meaning that the built-in objects within the object are created by injection.

So what is the relationship between the IOC and DI ?

IOC is a software design idea, DI is an implementation of this idea of software design. And the core mechanism in spring is di.

Ii. Examples of understanding IOC

classperson{PrivateWear clothe;  PublicWear getclothe () {returnclothe; }     Public voidsetclothe (Wear clothe) { This. clothe =clothe; }         Public voidWear () {clothe=Newsweater (); Clothe.    Wearclothe (); }   }Interfacewear{voidwearclothe ();}classSweaterImplementswear{@Override Public voidwearclothe () {//TODO auto-generated Method StubSystem.out.println ("Wear a sweater"); }    }classShirtImplementswear{@Override Public voidwearclothe () {//TODO auto-generated Method StubSystem.out.println ("Wear a shirt"); }    }
 Public Static void Main (string[] args) {        new person ();        Person. Wear ();    }

In the above code we define a wear interface, two implementations of the interface class sweater and shirt, in the person class wear need to invoke wear interface Wearclothe method, the traditional practice is to create the implementation class of the interface in the wear of person, This method is then called through the object pair. This object is created inside the person object, which controls the object, and when we need to call shirt's Wearclothe method, we need to change the code of the Wear method in the person class to create a shirt class object.

In the IOC's design philosophy, we will no longer create a reference object for the Clothe property inside the person, but instead create the object in the outside world and inject it into the person object. The code is implemented as follows:

To modify the wear method of the person class:

 Public void Wear () {        clothe. Wearclothe ();    }

Called in the Main method:

 Public Static void Main (string[] args) {        Wear Wearnew  sweater ();         New Person ();        Person.setclothe (sweater);        Person. Wear ();    }

By creating an implementation class object for an wear interface in the outside world, and then passing the object into the person object, the person object is inverted from the control of the Sweater object, and the person object is no longer created by the Wear interface implementation object. If we need to call shirt's wear method, we just need to change the creation of the shirt object, no longer need to modify the method inside the person class, it can be implemented.

That's what I understand. The IOC, in a nutshell, is simply to invert objects that are inherently controlled within the class and then inject them outside of the class, no longer controlled by the class itself, which is the essence of the IOC.

 

What exactly is IOC and di?

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.