SOLID 是一个缩写词,代表面向对象编程的五个基本原则,由 Robert C. Martin(鲍勃大叔)提出。在这里您可以阅读有关他的文章的更多信息。
这些原则旨在改进代码的结构和维护,使其更加灵活、可扩展且更易于理解。这些原则可以帮助程序员创建更有组织的代码、划分职责、减少依赖、简化重构过程并促进代码重用。
缩写中的“I”代表“接口隔离原则”。 Bob叔叔用来定义这个原则的一句话是:
“任何客户都不应该被迫依赖他们不使用的界面”
接口隔离原则解决了一个常见问题:接口过大迫使不需要它们的类实现不必要的实现。
想象一个应用程序中的身份验证系统,其中使用不同的方法来验证用户身份(例如,通过密码、通过生物识别、通过 QR 码)。
首先我们看一下这个类在Java和Typescript中不使用ISP的情况下的应用:
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"); } }
为了解决这个问题,我们可以将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 中应用'接口隔离原则”的详细内容。更多信息请关注PHP中文网其他相关文章!