Speaking of advanced {
Tagshow (Event)
} "> Language programming, we will think {
Tagshow (Event)
} "> Design Pattern. When it comes to design patterns, we will talk about how to solve coupling. And spring. one of the IOC containers of net is decoupling. Its most typical application is dependency injection (DI), which is one of the best decoupling methods. Next I will talk about the application scenarios of dependency injection.
I have simulated three different scenarios and can learn the importance of using dependency injection together.
The following are the conditions for the Application Scenario:Human Use {
Tagshow (Event)
} "> Tool labor.
- /** // <Summary>
- /// Abstract human
- /// </Summary>
- Public abstract class person
- {
- /** // <Summary>
- /// Work with tools
- /// </Summary>
- Public abstract void work ();
- }
- Public interface itool
- {
- /** // <Summary>
- /// Use the tool
- /// </Summary>
- Void usetool ();
- }
Copy code
Scenario 1: primitive society:The original man used spears for hunting.
- Public class spear: itool
- {
- Public void usetool ()
- {
- Console. writeline ("using spears ");
- }
- }
Copy code
Primitiveperson
- Public class primitiveperson: person
- {
- /** // <Summary>
- /// The primitive society uses Spears for hunting
- /// </Summary>
- Public override void work ()
- {
- // Know that hunting uses a spear and creates a spear.
- Itool tool = new spear ();
- Tool. usetool ();
- Console. writeline ("hunting with spears ");
- }
- }
Copy code
From the above code, we can easily see that, although the classic Rys replacement principle is used, the primitiveperson class is coupled with the spear class.
Attachment: 2009-10-26-1.gif
Scenario 2: Economic and Social:Farming With tools
- Public class Hoe: itool
- {
- Public void usetool ()
- {
- Console. writeline ("Use a hoe ");
- }
- }
Copy code
Toolfactory
- Public static class toolfactory
- {
- /** // <Summary>
- /// Factory manufacturing tools
- /// </Summary>
- /// <Returns> </returns>
- Public static itool createtool ()
- {
- Return new hoe (); // create a hoe
- }
- }
Copy code
Economy Person
- Public class economical myperson: person
- {
- /** // <Summary>
- // Economic and Social farming
- /// </Summary>
- Public override void work ()
- {
- // You don't need to know what tools are. You only need to know that the factory can buy the tools instead of making the tools by yourself.
- Itool tool = toolfactory. createtool ();
- Tool. usetool ();
- Console. writeline ("farming using economic and social tools ");
- }
- }
Copy code
From the code above, I can see that, using the classic factory model, economy person is only coupled with the factory, and does not care about how the factory makes tools. Scenario 3: current society:Work with tools
- Public class computer: itool
- {
- Public void usetool ()
- {
- Console. writeline ("use computer ");
- }
- }
Copy code Modernperson
- Public class modernperson: person
- {
- /** // <Summary>
- /// Obtain the tool from the external
- /// </Summary>
- Public itool tool {Get; set ;}
- /** // <Summary>
- /// The current user does not need to know where the computer came from and directly uses it for work.
- /// </Summary>
- Public override void work ()
- {
- // I don't know what tools are used and what tools are used, but they are just mechanized office work.
- Tool. usetool ();
- Console. writeline ("working with tools ");
- }
- }
Copy code App. config
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Configuration>
- <Configsections>
- <Sectiongroup name = "Spring">
- <Section name = "context" type = "Spring. Context. Support. contexthandler, spring. Core"/>
- <Section name = "objects" type = "Spring. Context. Support. defaultsectionhandler, spring. Core"/>
- </Sectiongroup>
- </Configsections>
- <Spring>
- <Context>
- <Resource uri = "config: // spring/objects"/>
- </Context>
- <Objects xmlns = "http://www.springframework.net">
- <Description> a simple example of control reversal </description>
- <Object ID = "computer" type = "springnetioc. Computer, springnetioc"/>
- <Object ID = "modernperson" type = "springnetioc. modernperson, springnetioc">
- <Property name = "tool" ref = "computer"/>
- </Object>
- </Objects>
- </Spring>
- </Configuration>
Copy code Program
- Class Program
- {
- Static void main (string [] ARGs)
- {
- Iapplicationcontext CTX = contextregistry. getcontext ();
- Person = (person) CTX. GetObject ("modernperson ");
- Person. Work ();
- Console. Readline ();
- }
- }
Copy code From the code above, we can see that the modernperson class does not need to know the specific tools to use when handing over objects to spring. Net containers for management. It is just a mechanical task. The configuration file determines what tools are used, and all objects are managed by the spring. Net container. This allows dynamic disassembly and assembly and Component reuse. I personally understand that dependency injection is an enhanced version of the reflection factory. |