(一):在Typescript和Java中应用“接口隔离原则”

(一):在typescript和java中应用“接口隔离原则”

概念

solid 是一个缩写词,代表面向对象编程的五个基本原则,由 robert c. martin(鲍勃大叔)提出。在这里您可以阅读有关他的文章的更多信息。
这些原则旨在改进代码的结构和维护,使其更加灵活、可扩展且更易于理解。这些原则可以帮助程序员创建更有组织的代码、划分职责、减少依赖、简化重构过程并促进代码重用。

缩写中的“i”代表“接口隔离原则”。 bob叔叔用来定义这个原则的一句话是:

“任何客户都不应该被迫依赖他们不使用的界面”

接口隔离原则解决了一个常见问题:接口过大迫使不需要它们的类实现不必要的实现。

实际应用

想象一个应用程序中的身份验证系统,其中使用不同的方法来验证用户身份(例如,通过密码、通过生物识别、通过 qr 码)。

首先我们看一下这个类在java和typescript中不使用isp的情况下的应用:

AppMall应用商店 AppMall应用商店

AI应用商店,提供即时交付、按需付费的人工智能应用服务

AppMall应用商店 56 查看详情 AppMall应用商店

立即学习“Java免费学习笔记(深入)”;

爪哇

interface authenticator {    boolean authenticatewithpassword(string userid, string password);    boolean authenticatewithbiometrics(string userid);    boolean authenticatewithqrcode(string qrcode);}class passwordauthenticator implements authenticator {    @override    public boolean authenticatewithpassword(string userid, string password) {        system.out.println("authenticating with password...");        return true;    }    @override    public boolean authenticatewithbiometrics(string userid) {        throw new unsupportedoperationexception("not implemented");    }    @override    public boolean authenticatewithqrcode(string qrcode) {        throw new unsupportedoperationexception("not implemented");    }}

打字稿

interface authenticator {  authenticatewithpassword(userid: string, password: string): boolean;  authenticatewithbiometrics(userid: string): boolean;  authenticatewithqrcode(qrcode: string): boolean;}class passwordauthenticator implements authenticator {  authenticatewithpassword(userid: string, password: string): boolean {    console.log("authenticating with password...");    return true;  }  authenticatewithbiometrics(userid: string): boolean {    throw new error("not implemented");  }  authenticatewithqrcode(qrcode: string): boolean {    throw new error("not implemented");  }}

问题:

未使用的方法:passwordauthenticator 类实现了对其功能没有意义的方法。维护麻烦:如果接口发生变化,所有实现类都需要更改,即使它们不使用新方法。单一责任违规:班级开始处理不应该属于他们的问题。

为了解决这个问题,我们可以将authenticator接口拆分为更小、更具体的接口。

爪哇

interface passwordauth {    boolean authenticatewithpassword(string userid, string password);}interface biometricauth {    boolean authenticatewithbiometrics(string userid);}interface qrcodeauth {    boolean authenticatewithqrcode(string qrcode);}class passwordauthenticator implements passwordauth {    @override    public boolean authenticatewithpassword(string userid, string password) {        system.out.println("authenticating with password...");        return true;    }}class biometricauthenticator implements biometricauth {    @override    public boolean authenticatewithbiometrics(string userid) {        system.out.println("authenticating with biometrics...");        return true;    }}class qrcodeauthenticator implements qrcodeauth {    @override    public boolean authenticatewithqrcode(string qrcode) {        system.out.println("authenticating with qr code...");        return true;    }}

打字稿

interface PasswordAuth {  authenticateWithPassword(userId: string, password: string): boolean;}interface BiometricAuth {  authenticateWithBiometrics(userId: string): boolean;}interface QRCodeAuth {  authenticateWithQRCode(qrCode: string): boolean;}class PasswordAuthenticator implements PasswordAuth {  authenticateWithPassword(userId: string, password: string): boolean {    console.log("Authenticating with password...");    return true;  }}class BiometricAuthenticator implements BiometricAuth {  authenticateWithBiometrics(userId: string): boolean {    console.log("Authenticating with biometrics...");    return true;  }}class QRCodeAuthenticator implements QRCodeAuth {  authenticateWithQRCode(qrCode: string): boolean {    console.log("Authenticating with QR Code...");    return true;  }}

重构的好处

特定接口:每个类仅实现它实际使用的方法。灵活性:添加新的身份验证方法或模式不会影响现有实现。更简单的维护:减少代码更改的影响,避免不必要的重构。

结论

以上就是(一):在Typescript和Java中应用“接口隔离原则”的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/387565.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年11月6日 14:21:14
下一篇 2025年11月6日 14:22:35

相关推荐

发表回复

登录后才能评论
关注微信