How to explain dependency injection? [Question, not answer]

Source: Internet
Author: User

A friend on the Internet asked me what the injection was like. I cannot express this meaning in a language that is as plain as possible. I said, I writeCodeIt may be easier to understand. However, the effect was not obvious.

The sample code is as follows:

 
Public interface idisplay {void display (string message);} public class LCD: idisplay {public void display (string message) {console. writeline ("display in LCD") ;}} public class CRT: idisplay {public void display (string message) {console. writeline ("display in CRT") ;}} public class desktop {public idisplay displaydevice = NULL; Public void showmessageonscreen (string message) {displaydevice. display (Message );}}

Breeze drunk 5:15:08

Public void main () {desktop = new desktop (); // injection LCD implementation desktop. displaydevice = new LCD (); desktop. showmessageonscreen ("display on LCD"); // inject the CRT into the desktop. displaydevice = new CRT (); desktop. showmessageonscreen ("display on CRT ");}

Additionally, I added some instructions:

 
Breeze drunk 5:15:08 pmdesktop (Computer) depends on the display device (displaydevice) to display information breeze drunk 5:15:31 pmdesktop left an interface idisplaycnblogs 5:16:01 ?? Breeze drunk 5:16:02 PM you can inject compliant implementation (LCD or CRT) breeze drunk 5:16:13 pm to this interface. Do you understand? Cnblogs 5:16:29 pm. Well, it's a bit refreshing. Drunk at 5:16:50 pm. Oh, breeze, drunk at 5:16:58 pm. Maybe it's too technical. Breeze, drunk at 5:17:09 pm. Do you understand object-oriented? Cnblogs 5:17:29 can only be said to understand qingbreeze drunk 5:18:49. We usually say injection is to change the behavior of the injected person through polymorphism.

 

When I said that, I became more and more colloquial. I know that I can't explain it clearly. The more I explain it, the more confusing it is. I was going to include Martin Fowler'sArticleSend it to him and give up later.

Further, how can we make a technical question clear to others?

Try to reach it...

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.