当前位置:网站首页>Interface isolation principle
Interface isolation principle
2022-06-27 01:17:00 【Hua Weiyun】
Interface isolation principle (Interface Segregation Principle), abbreviation ISP, Definition
The client should not rely on interfaces it does not need . The dependency of one class on another should be based on the smallest interface .
The above is the definition of interface isolation principle , In other words , It's a class , When implementing an interface , Don't implement methods that this class doesn't need at all .
Case study :
If a manufacturer wants to produce an electric rice cooker , What is the function of rice cooker in our cognition , for instance , The rice cooker has a cooking mode 、 There is a porridge cooking mode 、 Soup making mode , Of course, different rice cookers have different prices , They also have very different functions .
Now there are two types of rice cookers , One can cook 、 Cook porridge , One can cook rice and soup , The use UML How class diagrams should represent 
See class diagram , Because the interface of the rice cooker , All the functions of the rice cooker have been defined , If an electric rice cooker is to be produced , Then inherit the interface of rice cooker , But this rice cooker may not have some functions , such as JavaCooker Only the function of cooking rice and porridge , You can't cook soup , But he inherited Cooker Interface , The soup cooking interface must also be implemented , What to do with that ? return null, as follows
public interface Cooker { void cookRice(); void cookPorridge(); void cookSoup();}public class JavaCooker implements Cooker { @Override public void cookRice() { System.out.println(" Use JavaCooker Cook rice "); } @Override public void cookPorridge() { System.out.println(" Use JavaCooker Cook porridge "); } @Override public void cookSoup() { }}public class PyCooker implements Cooker{ @Override public void cookRice() { System.out.println(" Use PyCooker Cook rice "); } @Override public void cookPorridge() { } @Override public void cookSoup() { System.out.println(" Use PyCooker Boiling soup "); }}The above design , It is contrary to interface isolation , Interface isolation principle , If a class inherits an interface , These interfaces should be implemented , Instead of being empty or returning null, This way of writing , When the parent class calls , You may call an empty subclass , Cause incalculable consequences .
Then how to transform ?
The class diagram is as follows :
You should put the rice cooker , All functions , Define to an interface , If a class needs any function , Inherit the corresponding interface , Implement the corresponding method .
The significance of interface isolation principle :
The principle of interface isolation is to avoid the bloated interface , If you concentrate all functions into one interface , With the continuous increase of product functions , Functions within the interface are also increasing , The interface will become more and more bloated , Add a feature , Every subclass that implements this interface has to be modified .
“ Interface isolation ” In fact, it is the principle of customized service design . Use multiple inheritance of interfaces to realize the combination of different interfaces , So as to provide external combined functions — achieve “ Provide services on demand ”. The interface is about to be disassembled , But you can't tear it down too thin , There must be a standard , This is high cohesion . The interface should have some basic functions , Be able to complete a basic task alone .
边栏推荐
猜你喜欢

Summary of working at home during the epidemic | community essay solicitation

Custom MVC (imported into jar package) + difference from three-tier architecture + reflection + interview questions

一键加速索尼相机SD卡文件的复制操作,文件操作批处理教程

每日刷题记录 (五)

CH423要如何使用,便宜的国产IO扩展芯片

One click acceleration of Sony camera SD card file copy operation, file operation batch processing tutorial
![Find the minimum value in the rotation sort array ii[classical Abstract dichotomy + how to break the game left, middle and right are equal]](/img/75/05d5765588dfde971167fbc72e2aa8.png)
Find the minimum value in the rotation sort array ii[classical Abstract dichotomy + how to break the game left, middle and right are equal]

建模规范:环境设置

世界很大,有人把二维码纹在脖子上

getReader() has already been called for this request
随机推荐
微博评论高性能高可用架构
leetcode 1143. Longest common subsequence (medium)
Memcached foundation 7
memcached基础6
Topolvm: kubernetes local persistence scheme based on LVM, capacity aware, dynamically create PV, and easily use local disk
Memcached foundation 1
统一结果集的封装
IIS 部署静态网站和 FTP 服务
Keepalived 实现 Redis AutoFailover (RedisHA)14
memcached基础3
About Random Numbers
Esp32 experiment - self built web server distribution network 02
世界很大,有人把二维码纹在脖子上
Keepalived 实现 Redis AutoFailover (RedisHA)15
Keepalived 实现 Redis AutoFailover (RedisHA)12
Esp32-solo development tutorial to solve config_ FREERTOS_ UNICORE problem
Keepalived 实现 Redis AutoFailover (RedisHA)13
UVM中config_db机制的使用方法
Memcached foundation 2
Gaussian and Summary Stats