接口隔离原则是什么,以及如何在 C# 中实现它?
不应强迫客户端依赖于他们不使用的接口。
接口隔离原则规定不应强迫客户端实现他们不使用的接口。
与其使用一个庞大的接口,不如根据方法组使用许多小型接口,每个接口服务于一个子模块。
在接口隔离之前
示例
public interface IProduct {
int ID { get; set; }
double Weight { get; set; }
int Stock { get; set; }
int Inseam { get; set; }
int WaistSize { get; set; }
}
public class Jeans : IProduct {
public int ID { get; set; }
public double Weight { get; set; }
public int Stock { get; set; }
public int Inseam { get; set; }
public int WaistSize { get; set; }
}
public class BaseballCap : IProduct {
public int ID { get; set; }
public double Weight { get; set; }
public int Stock { get; set; }
public int Inseam { get; set; }
public int WaistSize { get; set; }
public int HatSize { get; set; }
}在接口隔离之后
示例
public interface IProduct {
int ID { get; set; }
double Weight { get; set; }
int Stock { get; set; }
}
public interface IPants {
int Inseam { get; set; }
int WaistSize { get; set; }
}
public interface IHat {
int HatSize { get; set; }
}
public class Jeans : IProduct, IPants {
public int ID { get; set; }
public double Weight { get; set; }
public int Stock { get; set; }
public int Inseam { get; set; }
public int WaistSize { get; set; }
}
public class BaseballCap : IProduct, IHat {
public int ID { get; set; }
public double Weight { get; set; }
public int Stock { get; set; }
public int HatSize { get; set; }
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP