当前位置:网站首页>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 !");
}
}边栏推荐
猜你喜欢

Loop filtering to uncover the technical principle behind video thousand fold compression

30讲 线性代数第二讲 矩阵

Anaconda安装、Jupyter Notebook默认启动路径修改及Nbextensions插件安装

Understand how to get started with machine learning to quantify transactions?

Linux环境安装mysql数据库详细教程(含卸载和密码重置过程)

无监督图像分类《SCAN:Learning to Classify Images without》代码分析笔记(1):simclr

Introduction to go language (VI) -- loop statement

Qubicle notes: Hello voxel
![PIL pilot image processing [1] - installation and creation](/img/21/34856f53c08a5369b834b7ca2fb07b.jpg)
PIL pilot image processing [1] - installation and creation

AHB2Standard_handshake_bridge 设计
随机推荐
【求助】请问如何让微信公众号文章在外部浏览器中打开后还能显示下方的精选留言?
[C language questions -- 10 simple questions for leetcode]
【四川大学】初试复试考研资料分享
NR LDPC punched
MySQL——事务
[laravel series 7.5] event system
[advanced MySQL] differences among 10 data types and how to optimize the table structure (3)
Off line operation of situation and policy (version) of Dayong in autumn 21 [standard answer]
Common - name of conference room
Programmers have changed dramatically in 10 years. Everything has changed, but it seems that nothing has changed
构建Web应用程序
MySQL - Basic select statement
cocan yocto buildroot
SLAM APP
Operator new and placement new
【Laravel系列7.5】事件系统
Hdu3527 (Hangdian) spy problem
Golang学习笔记—基础篇
Hyper parameter optimization of deep neural networks using Bayesian Optimization
YOLOv3 Pytorch代码及原理分析(一):跑通代码