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...