码迷,mamicode.com
首页 > 其他好文 > 详细

依赖倒置原则

时间:2016-08-18 19:42:29      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

A.高层次的模块不应该依赖于低层次的模块,他们都应该依赖于抽象。
B.抽象不应该依赖于具体实现,具体实现应该依赖于抽象。

简单的说就是要求对抽象进行编程,不要对实现进行编程,这样就降低了客户与实现模块间的耦合。

面向过程的开发,上层调用下层,上层依赖于下层,当下层剧烈变动时上层也要跟着变动,这就会导致模块的复用性降低而且大大提高了开发的成本。
面向对象的开发很好的解决了这个问题,一般情况下抽象的变化概率很小,让用户程序依赖于抽象,实现的细节也依赖于抽象。即使实现细节不断变动,只要抽象不变,客户程序就不需要变化。这大大降低了客户程序与实现细节的耦合度

1.在配置文件中配置类名以及相对应的DLL文件名称

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <appSettings>
     <add key="ApplePhone" value="DIP.ApplePhone,DIP"/>
    <add key="Honor" value="DIP.Honor,DIP"/>  </appSettings>
</configuration> 

 2.

抽象一个基础类:

    public abstract class BasePhone
    {
        public int Id { get; set; }
        public string Branch { get; set; }

        public abstract void System();

        public abstract void Call();
    }

派生于基础类

    public class ApplePhone : BasePhone
    {
        public override void System()
        {
            Console.WriteLine("IOS9.4");
        }

        public override void Call()
        {
            Console.WriteLine("Use {0} Call", this.GetType().Name);
        }
    }

    public class Honor : BasePhone
    {
        //public int Id { get; set; }
        //public string Branch { get; set; }

        public override void System()
        {
            Console.WriteLine("Android6.0");
        }

        public override void Call()
        {
            Console.WriteLine("Use {0} Call", this.GetType().Name);
        }
    }

    

    public class People
    {
        public int Id { get; set; }
        public string Name { get; set; }


        public void UsePhone(BasePhone phone)
        {
            Console.WriteLine("**********************************************");
            Console.WriteLine("This is {0} 在使用 {1}", this.Name, phone.GetType().Name);
            phone.System();
            phone.Call();
        }
    }

 

    /// <summary>
    /// 1 细节与抽象:类与接口/抽象类
    /// 2 依赖倒置原则(Dependence Inversion Principle)
    /// 上端和下端之间,应该依赖于抽象,而不依赖于细节
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(" 依赖倒置原则DIP");

            People people = new People()
            {
                Id = 26,
                Name = "Andy"
            };

           BasePhone phone = ObjectFactory.CreatePhoneReflection(PhoneType.ApplePhone);

           people.UsePhone(phone);
           Console.Read();
        }
    }

 

    public class ObjectFactory
    {
        public static BasePhone CreatePhone(PhoneType phoneType)
        {
            BasePhone phone = null;
            switch (phoneType)
            {
                case PhoneType.ApplePhone:
                    phone = new ApplePhone();
                    break;
                case PhoneType.Mi:
                    phone = new Mi();
                    break;
                case PhoneType.Honor:
                    phone = new Honor();
                    break;
                default:
                    throw new Exception("wrong phoneType");
            }
            return phone;
        }

        //使用反射获取类库中的类名

        public static BasePhone CreatePhoneReflection(PhoneType phoneType)
        {
            string classModule = ConfigurationManager.AppSettings[phoneType.ToString()];
            Assembly assembly = Assembly.Load(classModule.Split(‘,‘)[1]);
            Type type = assembly.GetType(classModule.Split(‘,‘)[0]);
            object oPhone = Activator.CreateInstance(type);
            return (BasePhone)oPhone;
        }
    }

    public enum PhoneType
    {
        ApplePhone,

        Honor
    }

依赖倒置原则

标签:

原文地址:http://www.cnblogs.com/zty1294625258/p/5784996.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!