新能源东莞网站建设技术支持,网站服务器年线太长,网站互点都是怎么做的,鹤壁建设网站推广渠道电话个人主页#xff1a;金鳞踏雨 个人简介#xff1a;大家好#xff0c;我是金鳞#xff0c;一个初出茅庐的Java小白 目前状况#xff1a;22届普通本科毕业生#xff0c;几经波折了#xff0c;现在任职于一家国内大型知名日化公司#xff0c;从事Java开发工作 我的博客金鳞踏雨 个人简介大家好我是金鳞一个初出茅庐的Java小白 目前状况22届普通本科毕业生几经波折了现在任职于一家国内大型知名日化公司从事Java开发工作 我的博客这里是CSDN是我学习技术总结知识的地方。希望和各位大佬交流共同进步 ~ 本文来自抖音《IT楠老师》设计模式课程下面是本人结合原课件的一些学习心得。 一、原理概述
客户端不应该强迫依赖它不需要的接口。
前面我提到理解接口隔离原则的关键就是理解其中的接口二字。
一组 API 接口集合单个 API 接口或函数OOP 中的接口概念 客户端不应该强迫依赖它不需要的接口。 接口隔离原则要求我们将大的、臃肿的接口拆分成更小、更专注的接口以确保类之间的解耦。这样客户端只需要依赖它实际使用的接口而不需要依赖那些无关的接口。 接口隔离原则有以下几个要点
将一个大的、通用的接口拆分成多个专用的接口。这样可以降低类之间的耦合度提高代码的可维护性和可读性。为每个接口定义一个独立的职责。这样可以确保接口的粒度适当同时也有助于遵循单一职责原则。在定义接口时要考虑到客户端的实际需求。客户端不应该被迫实现无关的接口方法。
二、经典案例
1. 案例一
假设我们正在开发一个机器人程序机器人具有多种功能如行走、飞行和工作。我们可以为这些功能创建一个统一的接口
public interface Robot {void walk();void fly();void work();
}
然而这个接口并不符合接口隔离原则因为它将多个功能聚合在了一个接口中。
对于那些只需要实现部分功能的客户端来说这个接口会导致不必要的依赖。为了遵循接口隔离原则我们应该将这个接口拆分成多个更小、更专注的接口。或者可以形象一点的说并不是所有的机器人都会飞
public interface Walkable {void walk();
}public interface Flyable {void fly();
}public interface Workable {void work();
}
现在我们可以根据需要为不同类型的机器人实现不同的接口。
例如对于一个只能行走和工作的机器人我们只需要实现Walkable和Workable接口
public class WalkingWorkerRobot implements Walkable, Workable {Overridepublic void walk() {// 实现行走功能}Overridepublic void work() {// 实现工作功能}
}
通过遵循接口隔离原则我们确保了代码的可维护性和可读性同时也降低了类之间的耦合度。在实际项目中要根据需求和场景来判断何时应用接口隔离原则。
接口隔离原则的关键是确保接口的职责清晰且单一但是这个单一又并非是只有一个的意思是相对的依托于实际的业务场景。
2. 案例二
我们还是结合一个例子来讲解。
微服务用户系统提供了一组跟用户相关的 API 给其他系统使用比如注册、登录、获取用户信息.....
public interface UserService {boolean register(String cellphone, String password);boolean login(String cellphone, String password);UserInfo getUserInfoById(long id);UserInfo getUserInfoByCellphone(String cellphone);
}
public class UserServiceImpl implements UserService {//...
}
现在我们的后台管理系统要实现删除用户的功能希望用户系统提供一个删除用户的接口。这个时候我们该如何来做呢你可能会说这不是很简单吗我只需要在 UserService 中新添加一个 deleteUserByCellphone() 或 deleteUserById() 接口就可以了。这个方法可以解决问题但是也隐藏了一些安全隐患。
删除用户是一个非常慎重的操作我们只希望通过后台管理系统来执行所以这个接口只限于给后台管理系统使用。如果我们把它放到 UserService 中那所有使用到 UserService 的系统都可以调用这个接口。不加限制地被其他业务系统调用就有可能导致误删用户。
当然最好的解决方案是从架构设计的层面通过接口鉴权的方式来限制接口的调用。不过如果暂时没有鉴权框架来支持我们还可以从代码设计的层面尽量避免接口被误用。我们参照接口隔离原则调用者不应该强迫依赖它不需要的接口将删除接口单独放到另外一个接口 RestrictedUserService 中然后将 RestrictedUserService 只打包提供给后台管理系统来使用。具体的代码实现如下所示
public interface UserService {boolean register(String cellphone, String password);boolean login(String cellphone, String password);UserInfo getUserInfoById(long id);UserInfo getUserInfoByCellphone(String cellphone);
}// 删除接口
public interface RestrictedUserService {boolean deleteUserByCellphone(String cellphone);boolean deleteUserById(long id);
}
public class UserServiceImpl implements UserService, RestrictedUserService {// ... 省略实现代码...
}
将 UserService 与 RestrictedUserService 接口隔离开来避免误调用删除的方法。
3. 案例三
假设我们的项目中用到了三个外部系统Redis、MySQL、Kafka。每个系统都对应一系列配置信息比如地址、端口、访问超时时间等。为了在内存中存储这些配置信息供项目中的其他模块来使用我们分别设计实现了三个 Configuration 类RedisConfig、MysqlConfig、KafkaConfig。
public class RedisConfig {private ConfigSource configSource; // 配置中心比如 zookeeperprivate String address;private int timeout;private int maxTotal;// 省略其他配置: maxWaitMillis,maxIdle,minIdle...public RedisConfig(ConfigSource configSource) {this.configSource configSource;}public String getAddress() {return this.address;}//... 省略其他 get()、init() 方法...public void update() {// 从 configSource 加载配置到 address/timeout/maxTotal...}
}public class KafkaConfig { //... 省略... }
public class MysqlConfig { //... 省略... }
现在我们有一个新的功能需求希望支持 Redis 和 Kafka 配置信息的热更新但是因为某些原因我们并不希望对 MySQL 的配置信息进行热更新。
所谓“热更新hot update”就是如果在配置中心中更改了配置信息我们希望在不用重启系统的情况下能将最新的配置信息加载到内存中也就是 RedisConfig、KafkaConfig 类中。
为了实现这样一个功能需求我们设计实现了一个 ScheduledUpdater 类以固定时间频率periodInSeconds来调用 RedisConfig、KafkaConfig 的 update() 方法更新配置信息。具体的代码实现如下所示
public interface Updater {void update();
}public class RedisConfig implemets Updater {//... 省略其他属性和方法...Overridepublic void update() { //... }
}public class KafkaConfig implements Updater {//... 省略其他属性和方法...Overridepublic void update() { //... }
}public class MysqlConfig { //... 省略其他属性和方法...
}public class ScheduledUpdater {private final ScheduledExecutorService executor Executors.newSingleThreadScheduledExecutor();;private long initialDelayInSeconds;private long periodInSeconds;private Updater updater;public ScheduleUpdater(Updater updater, long initialDelayInSeconds, long periodInSeconds) {this.updater updater;this.initialDelayInSeconds initialDelayInSeconds;this.periodInSeconds periodInSeconds;}public void run() {executor.scheduleAtFixedRate(new Runnable() {Overridepublic void run() {updater.update();}}, this.initialDelayInSeconds, this.periodInSeconds, TimeUnit.SECONDS);}
}public class Application {ConfigSource configSource new ZookeeperConfigSource(/* 省略参数 */);public static final RedisConfig redisConfig new RedisConfig(configSource);public static final KafkaConfig kafkaConfig new KakfaConfig(configSource);public static final MySqlConfig mysqlConfig new MysqlConfig(configSource);public static void main(String[] args) {ScheduledUpdater redisConfigUpdater new ScheduledUpdater(redisConfig, 300, 300);redisConfigUpdater.run();ScheduledUpdater kafkaConfigUpdater new ScheduledUpdater(kafkaConfig, 60, 60);redisConfigUpdater.run();}
}
刚刚的热更新的需求我们已经搞定了。
现在我们又有了一个新的监控功能需求。通过命令行来查看 Zookeeper 中的配置信息是比较麻烦的。所以我们希望能有一种更加方便的配置信息查看方式。
我们可以在项目中开发一个内嵌的 SimpleHttpServer输出项目的配置信息到一个固定的 HTTP 地址我们只需要在浏览器中输入这个地址就可以显示出系统的配置信息。不过出于某些原因我们只想暴露 MySQL 和 Redis 的配置信息不想暴露 Kafka 的配置信息。
为了实现这样一个功能我们还需要对上面的代码做进一步改造。
改造之后的代码如下所示
public interface Updater {void update();
}public interface Viewer {String outputInPlainText();MapString, String output();
}public class RedisConfig implemets Updater, Viewer {//... 省略其他属性和方法...Overridepublic void update() { //... }Overridepublic String outputInPlainText() { //... }Overridepublic MapString, String output() { //...}
}public class KafkaConfig implements Updater {//... 省略其他属性和方法...Overridepublic void update() { //... }
}public class MysqlConfig implements Viewer {//... 省略其他属性和方法...Overridepublic String outputInPlainText() { //... }Overridepublic MapString, String output() { //...}
}public class SimpleHttpServer {private String host;private int port;private MapString, ListViewer viewers new HashMap();public SimpleHttpServer(String host, int port) {//...}public void addViewers(String urlDirectory, Viewer viewer) {if (!viewers.containsKey(urlDirectory)) {viewers.put(urlDirectory, new ArrayListViewer());}this.viewers.get(urlDirectory).add(viewer);}public void run() { //... }
}public class Application {ConfigSource configSource new ZookeeperConfigSource();public static final RedisConfig redisConfig new RedisConfig(configSource);public static final KafkaConfig kafkaConfig new KakfaConfig(configSource);public static final MySqlConfig mysqlConfig new MySqlConfig(configSource);public static void main(String[] args) {ScheduledUpdater redisConfigUpdater new ScheduledUpdater(redisConfig, 300, 300);redisConfigUpdater.run();ScheduledUpdater kafkaConfigUpdater new ScheduledUpdater(kafkaConfig, 60, 60);redisConfigUpdater.run();SimpleHttpServer simpleHttpServer new SimpleHttpServer(“127.0.0.1”, 2389);simpleHttpServer.addViewer(/config, redisConfig);simpleHttpServer.addViewer(/config, mysqlConfig);simpleHttpServer.run();}
}
至此热更新和监控的需求我们就都实现了。
我们来回顾一下这个例子的设计思想。
我们设计了两个功能非常单一的接口Updater 和 Viewer。ScheduledUpdater 只依赖 Updater 这个跟热更新相关的接口不需要被强迫去依赖不需要的 Viewer 接口满足接口隔离原则。同理SimpleHttpServer 只依赖跟查看信息相关的 Viewer 接口不依赖不需要的 Updater 接口也满足接口隔离原则。
4. 提问getAndIncrement()是否满足接口隔离原则
java.util.concurrent 并发包提供了 AtomicInteger 这样一个原子类其中有一个函数 getAndIncrement() 是这样定义的给整数增加一并且返回未増之前的值。我的问题是这个函数的设计是否符合单一职责原则和接口隔离原则为什么
/*** Atomically increments by one the current value.* return the previous value*/
public final int getAndIncrement() {//...}
这个方法是为了解决多线程场景下的线程安全问题如果我们将其拆分成 get() 和 increment() 显然是不能解决这个问题的所以脱离业务讲设计都是刷流氓 文章到这里就结束了如果有什么疑问的地方可以在评论区指出~ 希望能和大佬们一起努力诸君顶峰相见 再次感谢各位小伙伴儿们的支持