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>
  • 相关阅读:
    ucore 物理内存探测 lab2 附录A&B
    git diff 笔记
    操作系统Lab1 详解(boot|kern/debug)
    ucore os 前初始化
    第五讲 计算机体系结构 内存层次
    Django 的学习(1) 从建立到数据库操作
    ucore os 初始化
    操作系统 Lab1
    makefile 语法笔记 3
    flex布局注意事项
  • 原文地址:https://www.cnblogs.com/king-tao/p/13412899.html
Copyright © 2011-2022 走看看