依赖倒置原则(二)-创新互联

依赖倒置原则就是抽象类和接口在使用时的一些规范和建议,我们的应用上层模块不直接依赖于下层模块,不具体依赖某个类或者对象,而是依赖于某个抽象。

为南通等地区用户提供了全套网页设计制作服务,及南通网站建设行业解决方案。主营业务为网站设计制作、网站设计、南通网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

接着上一节的Demo,我们有3个手机的对象。分别是LumiaPhone,Galaxy,ApplePhone,现在我们新增一个Student学生类,学生会使用手机。有可能使用LumiaPhone手机,也有可能使用Galaxy,也有可能使用

ApplePhone,那我们的Student看起来的得实现成这样:

       public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public void PlayPhone(LumiaPhone lumiaPhone)
        {
            Console.WriteLine($"{Name} use {lumiaPhone.Name}");
        }
        public void PlayApplePhone(ApplePhone applePhone)
        {
            Console.WriteLine($"{Name} use {applePhone.Name}");
        }
        public void PlayGalaxy(Galaxy galaxy)
        {
            Console.WriteLine($"{Name} use {galaxy.Name}");
        }
    }

具体使用时,我们可能会是这样: 先创建一个Student再根据学生使用的某种手机 调用不同的play方法

 LumiaPhone lumiaPhone = new LumiaPhone();
ApplePhone applePhone = new ApplePhone();
Galaxy galaxy = new Galaxy();
Student student = new Student();
student.PlayPhone(lumiaPhone);
student.PlayApplePhone(applePhone);
student.PlayGalaxy(galaxy);

这个时候假设Student旧的手机坏了,换了新的华为手机,那我们新增一个Honor对象

 public class Honor
    {
       public void System()
        {
            Console.WriteLine("Hua Wei Honor");
        }
    }

再修改Student类 新增一个方法

 
        public void PlayHonor(Honor honor)
        {
            Console.WriteLine($"{Name} use {honor.Name}");
        }

使用时

 Honor honor = new Honor();
student.PlayHonor(honor);

手机有太多种类,每次新增对象我们都要在Student中新增方法 在调用处进行手机的实例化,维护起来 非常麻烦,容易出错。不利于扩展,所以我们的Student 不应该依赖于具体的类,而是应该依赖抽象 上一遍提到的BasePhone,我们来改造下代码 Student会变成这样

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

        public void PlayPhone(BasePhone basePhone)
        {
            Console.WriteLine($"{Name} use {basePhone.Name}");
        }
        //public void PlayPhone(LumiaPhone lumiaPhone)
        //{
        //    Console.WriteLine($"{Name} use {lumiaPhone.Name}");
        //}

        //public void PlayApplePhone(ApplePhone applePhone)
        //{
        //    Console.WriteLine($"{Name} use {applePhone.Name}");
        //}

        //public void PlayGalaxy(Galaxy galaxy)
        //{
        //    Console.WriteLine($"{Name} use {galaxy.Name}");
        //}

        //public void PlayHonor(Honor honor)
        //{
        //    Console.WriteLine($"{Name} use {honor.Name}");
        //}
    }

我们的调用处

    static void Main(string[] args)

    {

      {

        LumiaPhone lumiaPhone = new LumiaPhone();

        ApplePhone applePhone = new ApplePhone();

        Galaxy galaxy = new Galaxy();

        Honor honor = new Honor();

        Student student = new Student();

        student.PlayPhone(lumiaPhone);

        student.PlayPhone(applePhone);

        student.PlayPhone(galaxy);

        student.PlayPhone(honor);

        //student.PlayPhone(lumiaPhone);

        //student.PlayApplePhone(applePhone);

        //student.PlayGalaxy(galaxy);

        //student.PlayHonor(honor);

      }

      Console.ReadKey();

    }

这个时候感觉Main还是依赖了具体的对象Student,以学生使用honor手机为例

                BasePhone honor = new Honor();
                Student student = new Student();
                student.PlayPhone(honor);

我们新增一个SimpleFactory及接口IPlayPhone

    public static class SimpleFactory
    {
        public static BasePhone CreatePhone()
        {
            return new Honor();
        }
        public static Student CreateStudent()
                {
                    return  new Student();
                }
    }

    public interface IPlayPhone
    {
        void PlayPhone(BasePhone basePhone);
    }

那我们的Main方法则变成

 static void Main(string[] args)
        {
            {
                BasePhone honor = SimpleFactory.CreatePhone();
                IPlayPhone student = SimpleFactory.CreateStudent();
                student.PlayPhone(honor);
                //Honor honor = new Honor();
                //Student student = new Student();
                //student.PlayPhone(honor);
                //student.PlayPhone(lumiaPhone);
                //student.PlayApplePhone(applePhone);
                //student.PlayGalaxy(galaxy);
                //student.PlayHonor(honor);
            }
            Console.ReadKey();
        }

我在这个demo SimpleFactory里直接返回了一个对象,项目中是可以通过读取配置文件反射来构造实的
这样只要通过改配置文件 就可以实现学生更换手机的功能,同样通过配置文件 也可以实现 老师使用手机只要我们新增一个Teacher类 实现IPlayPhone接口。其他代码不需要任何改动, 这就是我们原则 依赖抽象或接口 而不应该依赖于具体的细节的好处, 这里学生,老师其实就是上层,而我们的手机则是下层,上层不应该依赖下层 而是应该依赖抽象

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

本文标题:依赖倒置原则(二)-创新互联
网页链接:https://www.cdcxhl.com/article28/ppecp.html

成都网站建设公司_创新互联,为您提供响应式网站软件开发品牌网站建设搜索引擎优化外贸建站小程序开发

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站建设网站维护公司