翼度科技»论坛 编程开发 .net 查看内容

必知必会的设计原则——接口隔离原则

2

主题

2

帖子

6

积分

新手上路

Rank: 1

积分
6
设计原则系列文章

必知必会的设计原则——单一职责原则

必知必会的设计原则——开放封闭原则

必知必会的设计原则——依赖倒置原则

必知必会的设计原则——里氏替换原则

概述

1、 客户端不应该依赖它不需要的接口。
2、 一个类对另一个类的依赖应该建立在最小接口上。
3、接口应尽量细分,不要在一个接口中放很多方法。
接口分离和单一原则关系

单一职责:只做一件事 /影响类变化的原因只有一个。目的是你为了高内聚(模块内部的相似程度).
接口隔离:目的是为了低耦合(模块之间的依赖程度要低)。
未使用接口隔离原则的代码
  1. public interface IScore
  2.     {
  3.         void QueryScore();
  4.         void UpdateScore();
  5.         void AddScore();
  6.         void DeleteScore();
  7.         double GetSumScore();
  8.         double GetAvgScore();
  9.         void PrintScore();
  10.         void SendScore();
  11.     }
  12.     public class Teacher : IScore
  13.     {
  14.         public void AddScore()
  15.         {
  16.             throw new NotImplementedException();
  17.         }
  18.         public void DeleteScore()
  19.         {
  20.             throw new NotImplementedException();
  21.         }
  22.         public double GetAvgScore()
  23.         {
  24.             throw new NotImplementedException();
  25.         }
  26.         public double GetSumScore()
  27.         {
  28.             throw new NotImplementedException();
  29.         }
  30.         public void PrintScore()
  31.         {
  32.             throw new NotImplementedException();
  33.         }
  34.         public void QueryScore()
  35.         {
  36.             throw new NotImplementedException();
  37.         }
  38.         public void SendScore()
  39.         {
  40.             throw new NotImplementedException();
  41.         }
  42.         public void UpdateScore()
  43.         {
  44.             throw new NotImplementedException();
  45.         }
  46.     }
  47.     public class Student : IScore
  48.     {
  49.         public void AddScore()
  50.         {
  51.             throw new NotImplementedException();
  52.         }
  53.         public void DeleteScore()
  54.         {
  55.             throw new NotImplementedException();
  56.         }
  57.         public double GetAvgScore()
  58.         {
  59.             throw new NotImplementedException();
  60.         }
  61.         public double GetSumScore()
  62.         {
  63.             throw new NotImplementedException();
  64.         }
  65.         public void PrintScore()
  66.         {
  67.             throw new NotImplementedException();
  68.         }
  69.         public void QueryScore()
  70.         {
  71.             throw new NotImplementedException();
  72.         }
  73.         public void SendScore()
  74.         {
  75.             throw new NotImplementedException();
  76.         }
  77.         public void UpdateScore()
  78.         {
  79.             throw new NotImplementedException();
  80.         }
  81.     }
复制代码
以上定义成绩接口,接口里面包含的方法分别为查询成绩,添加成绩、修改成绩、删除成绩、成就求和,成绩求平均数、打印成绩、发送成绩。Teacher和student类都实现IScore的方法 ,有些方法教师或学生根本没必要实现。
使用接口隔离原则的代码
  1. public interface ITeacherScore
  2.     {
  3.         void AddScore();
  4.         void DeleteScore();
  5.         double GetSumScore();
  6.         double GetAvgScore();
  7.         void PrintScore();
  8.         void SendScore();
  9.     }
  10.     public class Teacher2 : ITeacherScore
  11.     {
  12.         public void AddScore()
  13.         {
  14.             throw new NotImplementedException();
  15.         }
  16.         public void DeleteScore()
  17.         {
  18.             throw new NotImplementedException();
  19.         }
  20.         public double GetAvgScore()
  21.         {
  22.             throw new NotImplementedException();
  23.         }
  24.         public double GetSumScore()
  25.         {
  26.             throw new NotImplementedException();
  27.         }
  28.         public void PrintScore()
  29.         {
  30.             throw new NotImplementedException();
  31.         }
  32.         public void SendScore()
  33.         {
  34.             throw new NotImplementedException();
  35.         }
  36.     }
  37.     public interface IStudentScore
  38.     {
  39.         void QueryScore();
  40.         void PrintScore();
  41.     }
  42.     public class Student2 : IStudentScore
  43.     {
  44.         public void PrintScore()
  45.         {
  46.             throw new NotImplementedException();
  47.         }
  48.         public void QueryScore()
  49.         {
  50.             throw new NotImplementedException();
  51.         }
  52.     }
复制代码
以上代码使用接口隔离原则后,接口职责很清晰,定义的接口不再像之前的大而全。
总结

接口隔离原则和单一职责原则很像,关于二者之间的关系在开头概述里面已经描述,如有疑问欢迎与我交流。

来源:https://www.cnblogs.com/mhg215/archive/2023/02/10/17104697.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具