当前位置:网站首页>Interface isolation principle
Interface isolation principle
2022-06-11 19:45:00 【Wenhao_ it】
All the following are summarized by me , Most of them come from learning .
Definition of interface isolation principle
Interface isolation principle (Interface Segregation Principle,ISP) Programmers are required to try their best to split the bloated interface into smaller and more specific interfaces , Let the interface contain only methods of interest to the customer .
2002 Robert ·C. Martin to “ Interface isolation principle ” Is defined as : The client should not be forced to rely on methods it does not use (Clients should not be forced to depend on methods they do not use). There is another definition of this principle : The dependency of one class on another should be based on the smallest interface (The dependency of one class to another one should depend on the smallest possible interface).
The meaning of the above two definitions is : To create the specialized interfaces they need for each class , Instead of trying to build a very large interface for all classes that depend on it to call .
Interface isolation principle and Single responsibility All are To improve class cohesion 、 Reduce the coupling between them , It embodies the idea of encapsulation , But the two are different :
- Principle of single responsibility Focus on responsibility , and Interface isolation principle Focus on the isolation of interface dependencies .
- Principle of single responsibility Mainly constraint classes , It aims at the implementation and details in the program ; Interface isolation principle Main constraint interface , Mainly for the construction of abstraction and program framework .
The advantages of the interface isolation principle
The principle of interface isolation is to constrain interfaces 、 Reduce the class's dependency on the interface , Follow the interface isolation principle as follows 5 advantage .
- Decompose the bulky interface into several interfaces with small granularity , Can prevent the spread of external changes , Improve the flexibility and maintainability of the system .
- Interface isolation improves the cohesion of the system , Reduced external interaction , It reduces the coupling of the system .
- If the granularity of an interface is well defined , It can guarantee the stability of the system ; however , If the definition is too small , It will cause too many interfaces , Complicate the design ; If the definition is too big , Less flexibility , Can't provide custom services , Bring unexpected risks to the whole project .
- Using multiple specialized interfaces can also reflect the level of objects , Because it can be inherited through the interface , Implement the definition of the total interface .
- It can reduce code redundancy in project engineering . Many different methods are usually placed in large interfaces , When implementing this interface , Forced to design redundant code .
How to implement the principle of interface isolation
In the specific application of interface isolation principle , It should be measured according to the following rules .
- The interface should be as small as possible , But to a limited extent . An interface serves only a sub module or business logic .
- Customize services for interface dependent classes . Provide only the methods the caller needs , Shield unwanted methods .
- Understand the environment , Refuse to follow blindly . Each project or product has selected environmental factors , The environment is different , The standard of interface splitting is different. We need to have a deep understanding of business logic .
- Improve cohesion , Reduce external interaction . Make the interface do the most things in the least way .

Learn from the code of other websites :
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();
}
}
// Input module interface
interface InputModule {
void insert();
void delete();
void modify();
}
// Statistics module interface
interface CountModule {
void countTotalScore();
void countAverage();
}
// Print module interface
interface PrintModule {
void printStuInfo();
void queryStuInfo();
}
// Implementation class
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(" Input module insert() Method is called !");
}
public void delete() {
Console.WriteLine(" Input module delete() Method is called !");
}
public void modify() {
Console.WriteLine(" Input module modify() Method is called !");
}
public void countTotalScore() {
Console.WriteLine(" Statistics module countTotalScore() Method is called !");
}
public void countAverage() {
Console.WriteLine(" Statistics module countAverage() Method is called !");
}
public void printStuInfo() {
Console.WriteLine(" Printing module printStuInfo() Method is called !");
}
public void queryStuInfo() {
Console.WriteLine(" Printing module queryStuInfo() Method is called !");
}
}边栏推荐
- 【mysql进阶】10种数据类型的区别以及如何优化表结构(三)
- YOLOv3 Pytorch代码及原理分析(一):跑通代码
- AHB2Standard_ handshake_ Bridge design
- 图床:PicGo+腾讯云+typora
- 556. 下一个更大元素 III-(31. 下一个排列)-两次遍历
- collect. stream(). Use of the collect() method
- SISO decoder for repetition (supplementary Chapter 4)
- Leetcode 1992. 找到所有的农场组(可以,一次过)
- 构建Web应用程序
- MySQL - transaction
猜你喜欢

巴比特 | 元宇宙每日必读:数字藏品二级市场乱象丛生,00后成新韭菜,监管迫在眉睫?...

Hyper parameter optimization of deep neural networks using Bayesian Optimization

谷歌提出超强预训练模型CoCa,在ImageNet上微调Top-1准确率达91%!在多个下游任务上SOTA!...

Software requirements engineering review
![[Sichuan University] information sharing of the first and second examinations](/img/0d/a6392e6b92bb689e90c038ffac9dd8.jpg)
[Sichuan University] information sharing of the first and second examinations

Common - name of conference room

WR | effect of micro nano plastic pollution in Jufeng formation of West Lake University on microbial flora and nitrogen removal function of Constructed Wetland

The latest test questions and answers for the eight major members (standard members) of Ningxia architecture in 2022

接口隔离原则

LNMP架构源码编译安装图文讲解(附带搭建论坛实验)
随机推荐
First acquaintance with enterprise platform
Longest strictly increasing subsequence
Go语言入门(六)——循环语句
556. 下一个更大元素 III-(31. 下一个排列)-两次遍历
AHB2Standard_ handshake_ Bridge design
In 2021, the global adult diaper revenue was about $11560million, which is expected to reach $15440million in 2028. From 2022 to 2028, the CAGR was 4.2%
Major work title and requirements of engineering earthquake resistance in autumn 21 [standard answer]
Proficient in xmake2
AHB2Standard_handshake_bridge 设计
CMU 15 - 445 cours de base de données Leçon 5 version texte - Pool tampon
30讲 线性代数第二讲 矩阵
CMU 15-445 数据库课程第五课文字版 - 缓冲池
Pyqt5 tips - button vertical display method, QT designer sets button vertical display.
【四川大学】初试复试考研资料分享
何恺明团队的“视频版本MAE”,高效视频预训练!Mask Ratio高达90%时效果也很好!...
[high precision] decimal integer addition
Raki's notes on reading paper: memory replace with data compression for continuous learning
基于飞桨搭建的多模态学习工具包PaddleMM
Detailed explanation of iSCSI (IV) -- actual configuration of iSCSI server
Template and requirements of curriculum design of reinforced concrete structure in autumn 21 of Dagong [standard answer]