
solid 是一个缩写词,代表面向对象编程的五个基本原则,由 robert c. martin(鲍勃大叔)提出。在这里您可以阅读有关他的文章的更多信息。
这些原则旨在改进代码的结构和维护,使其更加灵活、可扩展且更易于理解。这些原则可以帮助程序员创建更有组织的代码、划分职责、减少依赖、简化重构过程并促进代码重用。
缩写中的“i”代表“接口隔离原则”。 bob叔叔用来定义这个原则的一句话是:
“任何客户都不应该被迫依赖他们不使用的界面”
接口隔离原则解决了一个常见问题:接口过大迫使不需要它们的类实现不必要的实现。
想象一个应用程序中的身份验证系统,其中使用不同的方法来验证用户身份(例如,通过密码、通过生物识别、通过 qr 码)。
首先我们看一下这个类在java和typescript中不使用isp的情况下的应用:
立即学习“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");
}
}
为了解决这个问题,我们可以将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中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号