zoukankan      html  css  js  c++  java
  • Sping.NET之IOC,简单的业务场景应用梳理

    现实现一个小列子,引出IOC/DI的场景应用。

    一。学生通过骑自行车回学校

       1.1 返校交通工具接口,只定义了一个返校的方式方法,不同的交通工具实现改方法

      

    public interface ITool
        {
             void GoWay();
        }

    1.2 自行车实现上述接口

    1 public class Bike:ITool
    2     {
    3         public void GoWay() 
    4         {
    5             Console.WriteLine("骑自行车");
    6         }
    7     }

    1.3 学生通过骑自行车返校

     1  public class Student
     2     {
     3         public void GoSchool() 
     4         {
     5             ITool tool = new Bike();
     6             tool.GoWay();
     7             Console.WriteLine("学生回学校");
     8             /*
     9              * 典型的里式替换原则,子类可以替换了父类。但是Student类耦合了Bike类
    10              */
    11         }
    12     }

    二。学生通过大巴返校

    1 public class Bus:ITool
    2     {
    3         public void GoWay() 
    4         {
    5             Console.WriteLine("坐大巴");
    6         }
    7     }
    1  public static class ToolFactory
    2     {
    3         //简单交通工具工厂,创建交通工具对象
    4         public static ITool CreateBus() 
    5         {
    6             return new Bus();
    7         }
    8     }
     1 public class Student
     2     {
     3         public void GoSchool() 
     4         {
     5             ITool tool =ToolFactory.CreateBus();
     6             tool.GoWay();
     7             Console.WriteLine("学生回学校");
     8             /*
     9              * 使用的是简单的工厂模式生成交通工具对象。Student仅对对象工厂耦合,不关系工厂是怎么生产工具的
    10              */
    11         }
    12     }

    三。学生坐飞机返校。使用简单的属性注入依赖。

    1 public class Plane:ITool
    2     {
    3         public void GoWay() 
    4         {
    5             Console.WriteLine("坐飞机");
    6         }
    7     }
     1 public class Student
     2     {
     3         public ITool Tool;
     4         public void GoSchool() 
     5         {
     6             Tool.GoWay();
     7             Console.WriteLine("学生回学校");
     8             /*
     9              * Studnet类并不知道具体是什么交通工具返回学校,至于使用什么工具,有配置文件决定
    10              */
    11         }
    12     }
    1 public static void Main(string[] args)
    2         {
    3             IApplicationContext context = ContextRegistry.GetContext();
    4             //通过容器创建对象,并查找创建的对象是否有依赖,有则创建依赖,注入到对象中
    5             var student=context.GetObject("Student") as Student;
    6             student.GoSchool();
    7             Console.ReadKey();
    8         }

     xml配置:

    1 <?xml version="1.0" encoding="utf-8" ?>
    2 <objects>
    3     <object name="Student" type="SimpleIOC.model.Student,SimpleIOC">
    4         <property name="Tool" ref="Plane"/><!--属性注入-->
    5     </object>
    6     <object name="Plane" type="SimpleIOC.model.Plane,SimpleIOC"></object>
    7 </objects>
  • 相关阅读:
    [背包问题][二进制优化] Jzoj P4224 食物
    [并查集][排序] Jzoj P4223 旅游
    [哈夫曼树][优先队列] Bzoj P4198 荷马史诗
    [hash][差分][虚树] Jzoj P6011 天天爱跑步
    [dp] Jzoj P6012 荷马史诗
    [dp][递归] Jzoj P4211 送你一棵圣诞树
    [数学] Jzoj P3912 超氧化钾
    堆学习笔记(未完待续)(洛谷p1090合并果子)
    [AC自动机]luogu P2444 病毒
    [概率期望][DP]luogu P3830 随机树
  • 原文地址:https://www.cnblogs.com/king-tao/p/13412899.html
Copyright © 2011-2022 走看看