接口隔离原则
来源:dev.to
时间:2024-08-27 21:09:51 332浏览 收藏
怎么入门文章编程?需要学习哪些知识点?这是新手们刚接触编程时常见的问题;下面golang学习网就来给大家整理分享一些知识点,希望能够给初学者一些帮助。本篇文章就来介绍《接口隔离原则》,涉及到,有需要的可以收藏一下
任何客户端都不应该被迫依赖它不使用的方法
考虑办公空间的示例,其中使用对象表示各种输出设备
接口隔离原则之前:
i多功能接口
/** * @imultifunction interface has methods related to all output devices present in office space * for devices like printer, scanner, fax machines, etc */ public interface imultifunction { public void print(); public void getprintspooldetails(); public void scan(); public void scanphoto(); public void fax(); public void internetfax(); }
现在为各种设备实现上述通用接口
具有所有功能的 xeroxworkcenter 类
/** * * you must have seen xerox work station device which has all the features in one like printing, scanning, xerox, * fax etc */ public class xeroxworkcenter implements imultifunction { @override public void print() { // real printing code } @override public void getprintspooldetails() { // real get print spool details code } @override public void scan() { // read scanning code } @override public void scanphoto() { // real scan photo code } @override public void fax() { // real fax code } @override public void internetfax() { // real internet fax code } }
hpprinternscanner 类具有打印和扫描功能
public class hpprinternscanner implements imultifunction { @override public void print() { // real printing code } @override public void getprintspooldetails() { // real get print spool details code } @override public void scan() { // read scanning code } @override public void scanphoto() { // real scan photo code } //since hpprinternscanner has only printing and scanning abilities fax() and internetfax() will have empty body @override public void fax() {} @override public void internetfax() {} }
canonprinter 类只有打印功能
public class canonprinter implements imultifunction { @override public void print() { // real printing code } @override public void getprintspooldetails() { // real get print spool details code } //since the canonprinter has only printing ability rest of the method will have an empty body @override public void scan() {} @override public void scanphoto() {} @override public void fax() {} @override public void internetfax() {} }
isp违规识别技巧
- 胖接口(具有两个多方法声明的接口)
- 低内聚的接口(具有不太可能彼此相关的方法的接口)
- *空方法实现*(当类被迫实现他们不使用的方法时,他们将方法的实现留空)
接口隔离原则之后:
public interface iprint { public void print(); public void getprintspooldetails(); }
public interface iscan { public void scan(); public void scanphoto(); }
public interface ifax { public void fax(); public void internetfax(); }
/** * * you must have seen the xerox workstation device which has all the features in one like printing, scanning, xerox, fax, etc. */ public class xeroxworkcenter implements iprint,iscan,ifax { @override public void print() { // real printing code } @override public void getprintspooldetails() { // real get print spool details code } @override public void scan() { // read scanning code } @override public void scanphoto() { // real scan photo code ̰ } @override public void fax() { // real fax code } @override public void internetfax() { // real internet fax code } }
public class hpprinternscanner implements iprint,iscan { @override public void print() { // real printing code } @override public void getprintspooldetails() { // real get print spool details code } @override public void scan() { // read scanning code } @override public void scanphoto() { // real scan photo code } }
public class CanonPrinter implements IPrint { @Override public void print() { // real printing code } @Override public void getPrintSpoolDetails() { // real get print spool details code } }
每个接口都有一个单一的职责,并且现在更加干净。
isp 与其他 solid 原则的关系
单一责任
将接口划分为不同的接口后,现在所有接口(例如 iprint、iscan)都有单一职责
里氏替换
由于隔离,现在所有类(实现接口)都遵循里氏替换,因为所有子类型或实现类都可以用它们的接口引用变量替换
好了,本文到此结束,带大家了解了《接口隔离原则》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多文章知识!
声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
381 收藏
-
405 收藏
-
169 收藏
-
328 收藏
-
270 收藏
-
351 收藏
-
459 收藏
-
133 收藏
-
267 收藏
-
278 收藏
-
236 收藏
-
237 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习