当前位置:网站首页>接口隔离原则
接口隔离原则
2022-06-11 19:39:00 【Wenhao_it】
以下所有内容由我总结,大部分都是学习而来。
接口隔离原则的定义
接口隔离原则(Interface Segregation Principle,ISP)要求程序员尽量将臃肿庞大的接口拆分成更小的和更具体的接口,让接口中只包含客户感兴趣的方法。
2002 年罗伯特·C.马丁给“接口隔离原则”的定义是:客户端不应该被迫依赖于它不使用的方法(Clients should not be forced to depend on methods they do not use)。该原则还有另外一个定义:一个类对另一个类的依赖应该建立在最小的接口上(The dependency of one class to another one should depend on the smallest possible interface)。
以上两个定义的含义是:要为各个类建立它们需要的专用接口,而不要试图去建立一个很庞大的接口供所有依赖它的类去调用。
接口隔离原则和单一职责都是为了提高类的内聚性、降低它们之间的耦合性,体现了封装的思想,但两者是不同的:
- 单一职责原则注重的是职责,而接口隔离原则注重的是对接口依赖的隔离。
- 单一职责原则主要是约束类,它针对的是程序中的实现和细节;接口隔离原则主要约束接口,主要针对抽象和程序整体框架的构建。
接口隔离原则的优点
接口隔离原则是为了约束接口、降低类对接口的依赖性,遵循接口隔离原则有以下 5 个优点。
- 将臃肿庞大的接口分解为多个粒度小的接口,可以预防外来变更的扩散,提高系统的灵活性和可维护性。
- 接口隔离提高了系统的内聚性,减少了对外交互,降低了系统的耦合性。
- 如果接口的粒度大小定义合理,能够保证系统的稳定性;但是,如果定义过小,则会造成接口数量过多,使设计复杂化;如果定义太大,灵活性降低,无法提供定制服务,给整体项目带来无法预料的风险。
- 使用多个专门的接口还能够体现对象的层次,因为可以通过接口的继承,实现对总接口的定义。
- 能减少项目工程中的代码冗余。过大的大接口里面通常放置许多不用的方法,当实现这个接口的时候,被迫设计冗余的代码。
接口隔离原则的实现方法
在具体应用接口隔离原则时,应该根据以下几个规则来衡量。
- 接口尽量小,但是要有限度。一个接口只服务于一个子模块或业务逻辑。
- 为依赖接口的类定制服务。只提供调用者需要的方法,屏蔽不需要的方法。
- 了解环境,拒绝盲从。每个项目或产品都有选定的环境因素,环境不同,接口拆分的标准就不同深入了解业务逻辑。
- 提高内聚,减少对外交互。使接口用最少的方法去完成最多的事情。

借鉴了其他网站的代码:
public class ISPtest {
public static void main(String[] args) {
InputModule input = StuScoreList.getInputModule();
CountModule count = StuScoreList.getCountModule();
PrintModule print = StuScoreList.getPrintModule();
input.insert();
count.countTotalScore();
print.printStuInfo();
//print.delete();
}
}
//输入模块接口
interface InputModule {
void insert();
void delete();
void modify();
}
//统计模块接口
interface CountModule {
void countTotalScore();
void countAverage();
}
//打印模块接口
interface PrintModule {
void printStuInfo();
void queryStuInfo();
}
//实现类
class StuScoreList : InputModule, CountModule, PrintModule {
private StuScoreList() {
}
public static InputModule getInputModule() {
return (InputModule) new StuScoreList();
}
public static CountModule getCountModule() {
return (CountModule) new StuScoreList();
}
public static PrintModule getPrintModule() {
return (PrintModule) new StuScoreList();
}
public void insert() {
Console.WriteLine("输入模块的insert()方法被调用!");
}
public void delete() {
Console.WriteLine("输入模块的delete()方法被调用!");
}
public void modify() {
Console.WriteLine("输入模块的modify()方法被调用!");
}
public void countTotalScore() {
Console.WriteLine("统计模块的countTotalScore()方法被调用!");
}
public void countAverage() {
Console.WriteLine("统计模块的countAverage()方法被调用!");
}
public void printStuInfo() {
Console.WriteLine("打印模块的printStuInfo()方法被调用!");
}
public void queryStuInfo() {
Console.WriteLine("打印模块的queryStuInfo()方法被调用!");
}
}边栏推荐
- AHB2APB_bridge 设计
- MySQL——基本的Select语句
- Activate function formulas, derivatives, image notes
- SISO decoder for SPC (supplementary Chapter 1)
- 基于 Vue + Codemirror 实现 SQL 在线编辑器
- iMeta | 南科大夏雨组纳米孔测序揭示微生物可减轻高海拔冻土温室气体排放
- Find the maximum 3 same digits in the string
- CMU 15-445 database course lesson 5 text version - buffer pool
- Zzulioj 2903: the power of warriors
- 09 MySQL lock
猜你喜欢

Operator new and placement new

Pyqt5 tips - button vertical display method, QT designer sets button vertical display.

Qubicle notes: self set shortcut keys (attached with Lao Wang's self set shortcut key file)

懂机器学习如何入门量化交易?

Tensorflow---TFRecord文件的创建与读取

Software requirements engineering review
![[Lao Wang's fallacy of brain science] Why do blind people](/img/7c/98f27bb55a1a3b74c0ed8fd7fd2cc5.jpg)
[Lao Wang's fallacy of brain science] Why do blind people "seem" to be more "sensitive" than normal people?

Summary 111111111111111111111
![[untitled]](/img/02/49d333ba80bc6a3e699047c0c07632.png)
[untitled]

谷歌提出超强预训练模型CoCa,在ImageNet上微调Top-1准确率达91%!在多个下游任务上SOTA!...
随机推荐
Detailed explanation of iSCSI (IV) -- actual configuration of iSCSI server
程序员10年巨变,一切都变了又好像没变...
01. Telecommunications_ Field business experience
Questions and requirements of the "high rise building structure" assignment of Dayong in the 21st autumn [standard answer]
Introduction to go language (VI) -- loop statement
Usage of duck beak wire stripper
Merge sort divide and conquer
AHB2Standard_ handshake_ Bridge design
Raki's notes on reading paper: memory replace with data compression for continuous learning
谷歌提出超强预训练模型CoCa,在ImageNet上微调Top-1准确率达91%!在多个下游任务上SOTA!...
Qubicle notes: Hello voxel
Automated test requirements analysis
2022 the latest software testing classic summarized by major manufacturers. After reading it, I'm not afraid I won't get an offer
统一异常处理
WR | effect of micro nano plastic pollution in Jufeng formation of West Lake University on microbial flora and nitrogen removal function of Constructed Wetland
Use Mysql to determine the day of the week
Experiment report of basic mechanical experiment (II) of School of distance and continuing education, Dalian University of technology [standard answer]
【高精度】X进制整数加法
AHB2APB_bridge 设计
Proficient in xmake2