Dependency injection and control inversion in java

Source: Internet
Author: User

Before writing this article, let's talk a few things off the topic. In fact, I didn't want to learn so many programming languages. I originally wanted to be proficient in one language, but for work reasons, now I have to get started with the java Spring MVC Framework. When I started to learn Spring, I had some doubts about dependency injection and control reversal. After reading materials and programming practices, summarize some of your learning experiences as follows:

In object-oriented programming, we sometimes encounter this problem. The program of the high-level module calls the underlying module, when the object type of the underlying module changes, we have to change the program source code of the high-level module, for example:

1 public class WhereToGo{2     private Male MrKing = new Male();3     public void tellMe(){4         System.out.println("I should go to man's room");5     }6 }    
If it is a Female type object (woman), you have to change the source code of the program. The control of the program is actually in the hands of the Male and Female classes. In view of the above situation, we make the following changes: first define an interface:
1 public interface Gender{2     public void tell();3 }
Declare a method in the interface and declare this method for different implementations of the interface. RewriteThis method is as follows:
 1 public Male implements Gender{ 2     public void tell(){ 3         System.out.println("I should go to man's room"); 4     } 5 } 6   7 public Female implements Gender{ 8     public void tell(){ 9         System.out.println("I should go to woman's room");10     }11 }
The underlying Male and Female classes are re-implemented. The core of this class is to use interfaces to perform RewriteAnd then we override the WhereToGo class:
1 public class WhereToGo{2     private Gender g1;3     public void setGender(Gender g2){4         this.g1 = g2;5     }6     public void tellMe(){7         g1.tell();8     }9 }
This class defines the tellMe method as Male. tell () or Female. tell () through the interface, and the control is transferred to the interface Gender. Therefore, the control right is transferred from the bottom layer to the abstraction. Abstract: for example, to describe a person, a person consists of many attributes. We use two of them: gender and age to describe the person and define a class:
1 class person{2     private name;3     private age;4 }
In short, classes and interfaces are abstract and objects are implemented. The specific implementation is as follows:
1 WhereToGo where = new WhereToGo();2 where.setGender(new Female);3 where.tellMe();
If you are interested, try it on your own.

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.