一个重构:开闭原则案例

2023-11-09

原始代码

public class Alert {
  private AlertRule rule;
  private Notification notification;

  public Alert(AlertRule rule, Notification notification) {
    this.rule = rule;
    this.notification = notification;
  }

  public void check(String api, long requestCount, long errorCount, long durationOfSeconds) {
    long tps = requestCount / durationOfSeconds;
    if (tps > rule.getMatchedRule(api).getMaxTps()) {
      notification.notify(NotificationEmergencyLevel.URGENCY, "...");
    }
    if (errorCount > rule.getMatchedRule(api).getMaxErrorCount()) {
      notification.notify(NotificationEmergencyLevel.SEVERE, "...");
    }
  }
}

上面这段代码非常简单,业务逻辑主要集中在 check() 函数中。
当接口的 TPS 超过某个预先设置的最大值时,以及当接口请求出错数大于某个最大允许值时,就会触发告警,通知接口的相关负责人或者团队。

增加新功能(基于修改)

现在,如果我们需要添加一个功能,当每秒钟接口超时请求个数,超过某个预先设置的最大阈值时,我们也要触发告警发送通知。这个时候,我们该如何改动代码呢?主要的改动有两处:第一处是修改 check() 函数的入参,添加一个新的统计数据 timeoutCount,表示超时接口请求数;第二处是在 check() 函数中添加新的告警逻辑。具体的代码改动如下所示:

public class Alert {
  // ...省略AlertRule/Notification属性和构造函数...
  
  // 改动一:添加参数timeoutCount
  public void check(String api, long requestCount, long errorCount, long timeoutCount, long durationOfSeconds) {
    long tps = requestCount / durationOfSeconds;
    if (tps > rule.getMatchedRule(api).getMaxTps()) {
      notification.notify(NotificationEmergencyLevel.URGENCY, "...");
    }
    if (errorCount > rule.getMatchedRule(api).getMaxErrorCount()) {
      notification.notify(NotificationEmergencyLevel.SEVERE, "...");
    }
    // 改动二:添加接口超时处理逻辑
    long timeoutTps = timeoutCount / durationOfSeconds;
    if (timeoutTps > rule.getMatchedRule(api).getMaxTimeoutTps()) {
      notification.notify(NotificationEmergencyLevel.URGENCY, "...");
    }
  }
}

这样的代码修改实际上存在挺多问题的。一方面,我们对接口进行了修改,这就意味着调用这个接口的代码都要做相应的修改。另一方面,修改了 check() 函数,相应的单元测试都需要修改。

上面的代码改动是基于“修改”的方式来实现新功能的。如果我们遵循开闭原则,也就是“对扩展开放、对修改关闭”。那如何通过“扩展”的方式,来实现同样的功能呢?

重构版(基于扩展)

我们先重构一下之前的 Alert 代码,让它的扩展性更好一些。
重构的内容主要包含两部分:第一部分是将 check() 函数的多个入参封装成 ApiStatInfo 类;第二部分是引入 handler 的概念,将 if 判断逻辑分散在各个 handler 中。

public class Alert {
  private List<AlertHandler> alertHandlers = new ArrayList<>();
  
  public void addAlertHandler(AlertHandler alertHandler) {
    this.alertHandlers.add(alertHandler);
  }

  public void check(ApiStatInfo apiStatInfo) {
    for (AlertHandler handler : alertHandlers) {
      handler.check(apiStatInfo);
    }
  }
}

public class ApiStatInfo {//省略constructor/getter/setter方法
  private String api;
  private long requestCount;
  private long errorCount;
  private long durationOfSeconds;
}

public abstract class AlertHandler {
  protected AlertRule rule;
  protected Notification notification;
  public AlertHandler(AlertRule rule, Notification notification) {
    this.rule = rule;
    this.notification = notification;
  }
  public abstract void check(ApiStatInfo apiStatInfo);
}

public class TpsAlertHandler extends AlertHandler {
  public TpsAlertHandler(AlertRule rule, Notification notification) {
    super(rule, notification);
  }

  @Override
  public void check(ApiStatInfo apiStatInfo) {
    long tps = apiStatInfo.getRequestCount()/ apiStatInfo.getDurationOfSeconds();
    if (tps > rule.getMatchedRule(apiStatInfo.getApi()).getMaxTps()) {
      notification.notify(NotificationEmergencyLevel.URGENCY, "...");
    }
  }
}

public class ErrorAlertHandler extends AlertHandler {
  public ErrorAlertHandler(AlertRule rule, Notification notification){
    super(rule, notification);
  }

  @Override
  public void check(ApiStatInfo apiStatInfo) {
    if (apiStatInfo.getErrorCount() > rule.getMatchedRule(apiStatInfo.getApi()).getMaxErrorCount()) {
      notification.notify(NotificationEmergencyLevel.SEVERE, "...");
    }
  }
}

其中,ApplicationContext 是一个单例类,负责 Alert 的创建、组装(alertRule 和 notification 的依赖注入)、初始化(添加 handlers)工作。

public class ApplicationContext {
  private AlertRule alertRule;
  private Notification notification;
  private Alert alert;
  
  public void initializeBeans() {
    alertRule = new AlertRule(/*.省略参数.*/); //省略一些初始化代码
    notification = new Notification(/*.省略参数.*/); //省略一些初始化代码
    alert = new Alert();
    alert.addAlertHandler(new TpsAlertHandler(alertRule, notification));
    alert.addAlertHandler(new ErrorAlertHandler(alertRule, notification));
  }
  public Alert getAlert() { return alert; }

  // 饿汉式单例
  private static final ApplicationContext instance = new ApplicationContext();
  private ApplicationContext() {
    initializeBeans();
  }
  public static ApplicationContext getInstance() {
    return instance;
  }
}

public class Demo {
  public static void main(String[] args) {
    ApiStatInfo apiStatInfo = new ApiStatInfo();
    // ...省略设置apiStatInfo数据值的代码
    ApplicationContext.getInstance().getAlert().check(apiStatInfo);
  }
}

基于重构之后的代码,如果再添加上面讲到的那个新功能,每秒钟接口超时请求个数超过某个最大阈值就告警,我们又该如何改动代码呢?
主要的改动有下面四处。
第一处改动是:在 ApiStatInfo 类中添加新的属性 timeoutCount。
第二处改动是:添加新的 TimeoutAlertHander 类。
第三处改动是:在 ApplicationContext 类的 initializeBeans() 方法中,往 alert 对象中注册新的 timeoutAlertHandler。
第四处改动是:在使用 Alert 类的时候,需要给 check() 函数的入参 apiStatInfo 对象设置 timeoutCount 的值。

public class Alert { // 代码未改动... }
public class ApiStatInfo {//省略constructor/getter/setter方法
  private String api;
  private long requestCount;
  private long errorCount;
  private long durationOfSeconds;
  private long timeoutCount; // 改动一:添加新字段
}
public abstract class AlertHandler { //代码未改动... }
public class TpsAlertHandler extends AlertHandler {//代码未改动...}
public class ErrorAlertHandler extends AlertHandler {//代码未改动...}
// 改动二:添加新的handler
public class TimeoutAlertHandler extends AlertHandler {//省略代码...}

public class ApplicationContext {
  private AlertRule alertRule;
  private Notification notification;
  private Alert alert;
  
  public void initializeBeans() {
    alertRule = new AlertRule(/*.省略参数.*/); //省略一些初始化代码
    notification = new Notification(/*.省略参数.*/); //省略一些初始化代码
    alert = new Alert();
    alert.addAlertHandler(new TpsAlertHandler(alertRule, notification));
    alert.addAlertHandler(new ErrorAlertHandler(alertRule, notification));
    // 改动三:注册handler
    alert.addAlertHandler(new TimeoutAlertHandler(alertRule, notification));
  }
  //...省略其他未改动代码...
}

public class Demo {
  public static void main(String[] args) {
    ApiStatInfo apiStatInfo = new ApiStatInfo();
    // ...省略apiStatInfo的set字段代码
    apiStatInfo.setTimeoutCount(289); // 改动四:设置tiemoutCount值
    ApplicationContext.getInstance().getAlert().check(apiStatInfo);
}

重构之后的代码更加灵活和易扩展。如果我们要想添加新的告警逻辑,只需要基于扩展的方式创建新的 handler 类即可,不需要改动原来的 check() 函数的逻辑。而且,我们只需要为新的 handler 类添加单元测试,老的单元测试都不会失败,也不用修改。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

一个重构:开闭原则案例 的相关文章

  • 从原理聊JVM(一):染色标记和垃圾回收算法

    1 JVM运行时内存划分 1 1 运行时数据区域 方法区 属于共享内存区域 存储已被虚拟机加载的类信息 常量 静态变量 即时编译器编译后的代码等数据 运行时常量池 属于方法区的一部分 用于存放编译期生成的各种字面量和符号引用 JDK1 8之

随机推荐

  • 常见聚类算法及使用--均衡的迭代并减少聚类中心的层次聚类(BIRCH)

    前言 前面文章给大家介绍了 关于层次聚类算法的实现 那么本文给大家继续介绍层次聚类的优化算法 BIRCH 大家都知道像 K means 这样的聚类算法比较有局限性 而且在大数据场景下很难处理 特别是在有限的内存和较慢的CPU硬件条件下 我相
  • 安装/使用 pycurl 遇到的问题

    记录下安装 pycurl 出现的问题 问题3 ImportError pycurl libcurl link time ssl backend openssl is different from compile time ssl back
  • C语言指针总结

    文章目录 0 前言 1 指针的定义 2 指针的运算 3 指针与数组 3 1 数组指针 指向数组的指针 3 2 指针数组 4 指针与字符 5 指针与结构体 6 指针与const 常量指针与指针常量 6 1 指针常量 Constant Poin
  • 前端每日十题,题目答案来自(每日三加一网站)。

    2022 1 18 今日题目来源http www h camel com index html 每日三加一 答案选择评论区好的 同时写出自己的答案 转载加学习 加油 1 html 页面导入样式时 使用link和 import有事么区别 区别
  • 【牛客网刷题】VL8-VL10 generate for语句、比较数大小、function的使用

    写在前面 本系列博客记录牛客网刷题记录 日拱一卒 功不唐捐 目录 VL8 使用generate for语句简化代码 题目描述 输入描述 输出描述 RTL 设计 testbench 设计 仿真测试 VL9 使用子模块实现三输入数的大小比较 题
  • 软件测试度量的关键指标,软件测试度量指标简介

    1 测试度量的目的 测试度量活动首要考虑的是目的 测试中的度量一般有如下目的 判断测试的有效性 判断测试的完整性 判断工作产品的质量 分析和改进测试过程 2 度量内容 度量的数据构成一个层次化的体系 就是度量框架 框架的上层是度量指标 Fa
  • code embedding研究系列一-基于token的embedding

    Code Embedding系列 token embedding 1 Automated software vulnerability detection with machine learning 数据集来源 数据集预处理 分类方法 获取
  • Keepalived与HaProxy的协调合作原理分析

    Keepalived与HaProxy的协调合作原理分析 keepalived与haproxy合作场景 更好的理解方式 协调合作中考虑的问题 一 Keepalived 以TCP IP模型角度来分析 二 HaProxy 总结 协调合作中考虑的问
  • GoLang项目开发基础

    一 GOROOT GOROOT指的Golang语言的安装路径 即Golang语言内置程序库所在的位置 通常在安装时环境变量会设置好GOROOT路径 当开发时 import标准库时并不需要额外安装 当程序运行后 也会去GOROOT路径下寻找相
  • 11款插件让你的Chrome成为全世界最好用的浏览器|Chrome插件推荐

    文章来源 知乎 收录于 风云社区 SCOEE 提供mac软件下载 更多专题 可关注小编 微学徒 查看我的文章 也可上 风云社区 SCOEE 查找和下载相关软件资源 一 综合类 新买苹果电脑 mac系统中小白应该了解哪些东西 Mac新手必看教
  • 【云原生之Docker实战】使用Docker部署flatnotes笔记工具

    云原生之Docker实战 使用Docker部署flatnotes笔记工具 一 flatnotes介绍 1 1 flatnotes简介 1 2 flatnotes特点 二 本地环境介绍 2 1 本地环境规划 2 2 本次实践介绍 三 本地环境
  • git报错以及解决方法

    1 git报错1 在上传本地代码到github仓库时 出现下面这个问题 fatal remote origin already exists 先执行 git remote rm origin 再次添加 git remote add orig
  • Day4/7:2021-2-4-mybatis 狂神说 哔哩哔哩

    2021 2 4 博客 https blog csdn net DDDDeng article details 106927021 Mybatis 9 28 jdk1 8 mysql5 7 maven3 6 1 Maven项目对象模型 PO
  • java设计模式——建造者模式(Builder Pattern)

    在软件开发中 存在大量类似汽车一样的复杂对象 它们拥有一系列成员属性 这些成员属性中有些是引用类型的成员对象 而且在这些复杂对象中 还可能存在一些限制条件 如某些属性没有赋值则复杂对象不能作为一个完整的产品使用 有些属性的赋值必须按照某个顺
  • response.setContentType() ;参数说明

    response setContentType application octet stream 001 application x 001 301 application x 301 323 text h323 906 applicati
  • linux 下的绘图软件Visio——流程图,矢量图

    概述 说到画流程图 很多人第一反应是MS Visio 对于公司来讲 这确为较好的选择 但对个人偶尔应用 对于较简单的流程图 恐怕支付1000元 标准版或4000元 专业版的价格 远非良策 此时 不妨试一下免费 开源软件 或在线应用 来作为V
  • 关于2023年下半年计算机技术与软件专业技术资格(水平)考试报名工作有关事项的通知

    各市 区 人力资源和社会保障局 省级各有关部门人事处 中央驻陕有关单位人事处 各位考生 根据人力资源社会保障部办公厅 关于2023年度专业技术人员职业资格考试计划及有关事项的通知 人社厅发 2023 3号 全国计算机专业技术资格考试办公室
  • C# NPOI 设置(.xlsx) 【单元格填充】以及【字体颜色】

    C NPOI 设置 xlsx 单元格填充 以及 字体颜色 写在前面 因为我需要用到NPOI处理 xlsx文件 需要设置单元格填充及字体颜色 期间网上搜索的时候很麻烦 结果五花八门 提炼一下 记录在此 引用 using NPOI XSSF U
  • XSS绕过技巧总结

    XSS绕过技巧 作者 白泽Sec安全实验室 前言 XSS是Web应用程序中常见的漏洞之一 网站管理员可以通过用户输入过滤 根据上下文转换输出数据 正确使用DOM 强制执行跨源资源共享 CORS 策略以及其他的安全策略来规避XSS漏洞 尽管现
  • 一个重构:开闭原则案例

    原始代码 public class Alert private AlertRule rule private Notification notification public Alert AlertRule rule Notificatio