0

0

接口隔离原则

王林

王林

发布时间:2024-08-26 08:00:04

|

612人浏览过

|

来源于dev.to

转载

接口隔离原则

任何客户端都不应该被迫依赖它不使用的方法

考虑办公空间的示例,其中使用对象表示各种输出设备

接口隔离原则之前

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 类具有打印和扫描功能

易优微信工程机械小程序模板
易优微信工程机械小程序模板

易优小程序是基于前端开源小程序+后端易优cms+标签化API接口,是一套开源、快速搭建个性化需求的小程序CMS。轻量级TP底层框架,前后端分离,标签化API接口可对接所有小程序,支持二次开发。即使小白用户也能轻松搭建制作一套完整的线上版小程序。微信工程机械小程序模板主要特点:1、代码开源,支持二次修改。2、微信原生写法,兼容性更好,代码可读性更强。3、功能接口完整,支持eyoucms大部分功能ap

下载
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)都有单一职责

里氏替换
由于隔离,现在所有类(实现接口)都遵循里氏替换,因为所有子类型或实现类都可以用它们的接口引用变量替换

相关文章

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
硬盘接口类型介绍
硬盘接口类型介绍

硬盘接口类型有IDE、SATA、SCSI、Fibre Channel、USB、eSATA、mSATA、PCIe等等。详细介绍:1、IDE接口是一种并行接口,主要用于连接硬盘和光驱等设备,它主要有两种类型:ATA和ATAPI,IDE接口已经逐渐被SATA接口;2、SATA接口是一种串行接口,相较于IDE接口,它具有更高的传输速度、更低的功耗和更小的体积;3、SCSI接口等等。

1021

2023.10.19

PHP接口编写教程
PHP接口编写教程

本专题整合了PHP接口编写教程,阅读专题下面的文章了解更多详细内容。

64

2025.10.17

php8.4实现接口限流的教程
php8.4实现接口限流的教程

PHP8.4本身不内置限流功能,需借助Redis(令牌桶)或Swoole(漏桶)实现;文件锁因I/O瓶颈、无跨机共享、秒级精度等缺陷不适用高并发场景。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

415

2025.12.29

高德地图升级方法汇总
高德地图升级方法汇总

本专题整合了高德地图升级相关教程,阅读专题下面的文章了解更多详细内容。

0

2026.01.16

全民K歌得高分教程大全
全民K歌得高分教程大全

本专题整合了全民K歌得高分技巧汇总,阅读专题下面的文章了解更多详细内容。

0

2026.01.16

C++ 单元测试与代码质量保障
C++ 单元测试与代码质量保障

本专题系统讲解 C++ 在单元测试与代码质量保障方面的实战方法,包括测试驱动开发理念、Google Test/Google Mock 的使用、测试用例设计、边界条件验证、持续集成中的自动化测试流程,以及常见代码质量问题的发现与修复。通过工程化示例,帮助开发者建立 可测试、可维护、高质量的 C++ 项目体系。

10

2026.01.16

java数据库连接教程大全
java数据库连接教程大全

本专题整合了java数据库连接相关教程,阅读专题下面的文章了解更多详细内容。

32

2026.01.15

Java音频处理教程汇总
Java音频处理教程汇总

本专题整合了java音频处理教程大全,阅读专题下面的文章了解更多详细内容。

14

2026.01.15

windows查看wifi密码教程大全
windows查看wifi密码教程大全

本专题整合了windows查看wifi密码教程大全,阅读专题下面的文章了解更多详细内容。

42

2026.01.15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号