当前位置:网站首页>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 .
边栏推荐
- XSS attack notes (Part 1)
- Ml: a detailed introduction to the division of the top ten roles, backgrounds, responsibilities and outputs of the machine learning engineering team
- Bootstrapblazor + FreeSQL actual combat chart usage (2)
- Kept to implement redis autofailover (redisha) 15
- Review the old and know the new -- constant renewal at normal temperature
- Memcached Foundation
- 乔治·华盛顿大学 : Hanhan Zhou | PAC:多智能体强化学习中具有反事实预测的辅助价值因子分解
- Implementation of ARP module in LwIP
- Solve the problem that stc8g1k08 program cannot run and port configuration
- buuctf-pwn write-ups (6)
猜你喜欢

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

Law of Large Numbers

Live review | Ziya &ccf TF: Discussion on software supply chain risk management technology under cloud native scenario

JSON解析,ESP32轻松获取时间气温和天气

Tsinghua & Zhiyuan | cogview2: faster and better text image generation model

buuctf-pwn write-ups (6)

做了两天的唯美蝴蝶动画

Law of Large Numbers

用代码生成流程图,Markdown的使用方法

Central Limit Theorem
随机推荐
Flink 实战问题(七):No Watermark(Watermarks are only available EventTime is used)
乔治·华盛顿大学 : Hanhan Zhou | PAC:多智能体强化学习中具有反事实预测的辅助价值因子分解
寻找旋转排序数组中的最小值 II[经典抽象二分 + 如何破局左中右三者相等]
At present, which securities company is the best and safest to open an account for stock speculation?
Operating instructions and Q & A of cec-i China learning machine
Play OLED, u8g2 animation, increasing numbers, random triangles, etc
memcached基础5
超越锂电池——未来电池的概念
memcached基础3
getReader() has already been called for this request
Gaussian and Summary Stats
对象的访问机制及其他
Memcached foundation 6
网上开通证券账户安全吗 手机炒股靠谱吗
These 10 copywriting artifacts help you speed up the code. Are you still worried that you can't write a copywriting for US media?
架构实战营模块五作业
Solve the problem that only one line of text is displayed or not displayed in u8glib
自定义类加载器对类加密解密
Memcached foundation 5
Solve the problem that stc8g1k08 program cannot run and port configuration