Understanding of Java control inversion and dependency injection

Source: Internet
Author: User

Start learning Java Fundamentals and ideas ~

The inversion of Control (IOC) is the core idea of the spring framework, which, in my own words, is that you have to do one thing, don't be yourself new, you say what you want to do, and then outsource it.
Dependency Injection (DI) in my shallow thoughts, it is through the reference of interfaces and the expression of construction methods, that something is done in turn to the place that needs to be used.

It's good to do this: To do a single duty, and to improve reusability, after decoupling, any way you can do it, using the interface's reference call method, never need to change

Lift a chestnut:

Write an interface, say we go shopping ~

 Public Interface ishopping {    void Drive ();    String money ();    }

In the realization of it two: the rich people shopping, no money to shop--! This chestnut is lifted

 public  class  RichMan "implements   ishopping{@Override  public  void   drive () {System.out.println ( "Drive by Benz" );        } @Override  public   String money () {        System.out.println ( Lot's money " return  "10000" ; }}
 Public class Implements ishopping{    @Override    publicvoid Drive () {        System.out.println (" Take a walk! " );    }    @Override    public  String Money () {        System.out.println ("nothing");         return "1";    }}

Then we go out to play, by the way shopping a bit ~

 Public class Play {        private  ishopping shoppingsomething;    
Using the construction method, the incoming public Play (ishopping shoppingsomething) { this is implemented. shoppingsomething = shoppingsomething; } Public void Withgirlfriend () { shoppingsomething.drive (); Shoppingsomething.money (); }
}

The way you want to use it, inject it in a container, and here's how to inject it under the simulation:

 Public class Containner {        public  Play getshopping () {        returnnew Play (  New  RichMan ());}    }

Test it:

 Public Static void Main (string[] args) {        new  Containner ();         =  containner.getshopping ();        Play.withgirlfriend ();        }

The result is:

Drive by Benz
Lot ' s of money

If you rely on the injected configuration again, the implementation of the poor, then we do not have to change the code, the result will change ~ Well, it should be understood.

Understanding of Java control inversion and dependency injection

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.