DI和IOC

DI和IOC是差不多的概念。
一个重要特征是接口依赖,是把对象关系推迟到运行时去确定.
DI是一个初始化实例的过程,分为三种1.setter based 2.constructor based 3.interface based,在spring 中就是应用前两种;但又不仅仅是初始化实例,而且是运用接口的概念去实现这种注入依赖。静态地看,只是依赖一个接口,但实际运行起来,是依赖一个实现了该接口的具体类。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DIApp
{
    //Model类
    public class Movie
    {
        public int MovieID { get; set; }
        public string MovieName { get; set; }
        public decimal MovieCost { get; set; }
        public decimal MoviePrice { set; get; }
    }

    //模型数据类
    public static class Movies
    {
        //模型数据数组,这里为了简化数据库数据获取,用静态数组替代,实际可以实现get/set方法
        public static Movie[] movies = {
            new Movie {MovieID = 1, MovieName = "猩猿崛起II", MovieCost = 80000000.00M, MoviePrice = 80.00M},
            new Movie {MovieID = 2, MovieName = "生化危机", MovieCost = 5000000.00M, MoviePrice = 50.00M},
            new Movie {MovieID = 3, MovieName = "大话西游", MovieCost = 3000000.00M, MoviePrice = 35.00M},
            new Movie {MovieID = 4, MovieName = "叶问II", MovieCost = 58000000.00M, MoviePrice = 80.00M},
            new Movie {MovieID = 5, MovieName = "魔戒", MovieCost = 180000000.00M, MoviePrice = 60.00M},
            new Movie {MovieID = 6, MovieName = "哈利波特", MovieCost = 170000000.00M, MoviePrice = 60.00M},
            new Movie {MovieID = 7, MovieName = "高考1977", MovieCost = 9000000.00M, MoviePrice = 60.00M},
            new Movie {MovieID = 8, MovieName = "纵横天下", MovieCost = 3150000.00M, MoviePrice = 40.00M},
            new Movie {MovieID = 9, MovieName = "醉拳", MovieCost = 2000000.00M, MoviePrice = 40.00M}
        };
        //public IEnumerable<Movie> movies { get; set; }
    }

    //负责Movie计算逻辑的接口实现
    public interface IMoviesCalculator
    {
        decimal CalculateMovies(IEnumerable<Movie> movies);
    }

    //负责Movie计算逻辑的类实现
    public class MoviesCalculator : IMoviesCalculator
    {
        public decimal CalculateMovies(IEnumerable<Movie> movies)
        {
            return movies.Sum(p => p.MoviePrice);
        }
    }

    //Movie导购车类
    public class MoviesCart
    {
        private IMoviesCalculator moviesCalculator;

        public MoviesCart(IMoviesCalculator moviesCalculator)
        {
            this.moviesCalculator = moviesCalculator;
        }

        public decimal CalculateMovies()
        {
            return moviesCalculator.CalculateMovies(Movies.movies);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            IMoviesCalculator moviesCalculator = new MoviesCalculator();
            MoviesCart moviesCart = new MoviesCart(moviesCalculator);
            Console.WriteLine(moviesCart.CalculateMovies());
            Console.ReadKey(true);
        }
    }
}

从上面就可以看出任何实现IMoviesCalculator接口的类都可以使用导购车,这样当我们以后想计算电视据或者其他节目的价格时,要改动的代码就少很多,提高了代码的复用性。

 


IOC就是面向接口编程的应用
1.一个通常的做法(非面向接口编程的方法),对象A依赖对象B,B会作为A的属性,可以理解为A直接控制B;
2.IOC的做法,对象A依赖于接口C,而不直接依赖于实现了接口C的B,也就是A不能直接控制是哪个具体实现了C的B去做相应的事情,而是由我们控制可以由哪个实现了接口C的B去处理,也就是控制反过来了,是由B来决定了,而不是由A,实现就是面向接口编程。

 一个很好的例子:http://blog.csdn.net/wanzhuan2010/article/details/7763280

IOC是个比较大的概念, 以下是它的两种实现:

依赖查找:容器提供回调接口和上下文环境给组件。 EJB 和 Apache Avalon 都是使用这种方式。

依赖注入(DI):组件不做定位查询,只是提供普通的 Java 方法让容器去决定依赖关系。容器全权负责组件的装配,它会把符合依赖关系的对象通过 JavaBean 属性或者构造子传递给需要的对象。通过 JavaBean 属性注射依赖关系……

posted @ 2015-08-03 10:40  leftcity  阅读(310)  评论(0编辑  收藏  举报