带你读《2022技术人的百宝黑皮书》——谈一谈凑单页的那些优雅设计(5)

简介: 带你读《2022技术人的百宝黑皮书》——谈一谈凑单页的那些优雅设计(5)

带你读《2022技术人的百宝黑皮书》——谈一谈凑单页的那些优雅设计(4)https://developer.aliyun.com/article/1338378?groupCode=taobaotech

各个域需要满足如下条件:

  1. 支持单个处理和批量处理
  2. 支持提前阻断
  3. 支持前置判断是否需要处理

 

image.png处理类类图如下

 

 

【ChainBaseHandler】:核心处理类

【CartHandler】:加购域处理类

【ItemSupplementHandler】:商品域处理类

【RankingHandler】:榜单域处理类

【RequestHanlder】:参数域处理类

 

我们首先来看核心处理层


 

 public class ChainBaseHandler<T extends Context> { 2  /**
  * 任务执行
  * @param context
 */
  public void execute(T context) {
  List<String> executeCommands = Lists.newArrayList();
  for (Command<T> c : commands) {
  try {
// 前置校验
  if (!c.check(context)) {
  continue;
 }
  // 执行
  boolean isContinue = timeConsuming(() -> execute(context, c), c, executeCommands);
  if (!isContinue) {
break;
}
} catch (Throwable e) {
// 打印异常信息
GatherContext.debug("exception", c.getClass().getSimpleName()); GatherContext.error(c.getClass().getSimpleName() + " catch exception", e);
}
}
// 打印个命令任务耗时
GatherContext.debug(this.getClass().getSimpleName() + "-execute", executeCommands);
)
)

 


中间的timeConsuming方法用来计算耗时,耗时需要前后包裹执行方法

 

  private boolean timeConsuming(Supplier<Boolean> supplier, Command<T> c, List<String> execute- Commands) {
  long startTime = System.currentTimeMillis();
  boolean isContinue = supplier.get();
  long endTime = System.currentTimeMillis();
  long timeConsuming = endTime - startTime;
  executeCommands.add(c.getClass().getSimpleName() + ":" + timeConsuming);
  return isContinue;
 }

 


具体执行如下:

 

 

 /**
  * 执行每个命令
  * @return 是否继续执行4 */
  private <D extends ContextData> boolean execute(Context context, Command<T> c) {
  if (context instanceof MuchContext) {
  return execute((MuchContext<D>) context, c); 8 }
  if (context instanceof OneContext) {
  return execute((OneContext<D>) context, c); 11 }
  return true;
)
/**
* 单数据执行
* @return 是否继续执行
*/
private <D  extends ContextData> boolean execute(OneContext<D> oneContext, Command<T>  c)   {     if (Objects.isNull(oneContext.getData())) {
return false;
}
if (c instanceof CommonCommand) {
return ((CommonCommand<OneContext<D>>) c).execute(oneContext);
}
return true;
}
/**
* 批量数据执行
* @return 是否继续执行
*/
private <D  extends  ContextData>  boolean  execute(MuchContext<D>  muchContext,  Command<T>  c)  { if (CollectionUtils.isEmpty(muchContext.getData())) {
return false;
}
if (c instanceof SingleCommand) {
muchContext.getData().forEach(data -> ((SingleCommand<MuchContext<D>, D>) c).execute (data, muchContext));
return true;
}
if (c instanceof CommonCommand) {
return ((CommonCommand<MuchContext<D>>) c).execute(muchContext);
}
return true;

 


 

 

带你读《2022技术人的百宝黑皮书》——谈一谈凑单页的那些优雅设计(6)https://developer.aliyun.com/article/1338376?groupCode=taobaotech


相关文章
|
前端开发 算法 PHP
Jetpack Compose Runtime : 声明式 UI 的基础
Jetpack Compose Runtime : 声明式 UI 的基础
415 0
|
人工智能 运维 监控
从大规模恶意攻击 DeepSeek 事件看 AI 创新隐忧:安全可观测体系建设刻不容缓
在近来发生的 DeepSeek 遭遇的安全事件中,我们可以看到当前人工智能行业在网络安全方面的脆弱性,同时也为业界敲响了警钟。唯有通过全行业的协同努力,加强整体、完善的网络安全可观测建设,才能为 AI 技术的创新和发展构建一个安全而稳固的环境。我们期盼并相信,在攻克这些网络安全难题之后,AI 创新将迎来更加安全、灿烂的未来。
832 105
|
Shell
Zookeeper常用命令
Zookeeper常用命令
282 1
|
Java 测试技术 C#
浅谈 C# 13 中的 params 集合
浅谈 C# 13 中的 params 集合
304 5
|
机器学习/深度学习
苹果发布高效双EMA梯度优化方法,适配Transformer、Mamba模型
苹果公司在arXiv上发布论文《The AdEMAMix Optimizer: Better, Faster, Older》,提出了一种基于双指数移动平均(EMA)的新型优化器AdEMAMix。该优化器通过使用快速和慢速EMA,同时利用近期和远期梯度信息,显著提升了模型训练的速度和效果。实验表明,AdEMAMix在语言建模和图像分类等任务上表现出色,尤其在大型语言模型的训练中,相比传统优化器如AdamW,训练效率提高了95%。
405 32
|
SQL 缓存 关系型数据库
mysql性能优化-慢查询分析、优化索引和配置
mysql性能优化-慢查询分析、优化索引和配置
640 1
|
存储 Java API
优雅地使用Java Map,通过掌握其高级特性和技巧,让代码更简洁。
【10月更文挑战第19天】本文介绍了如何优雅地使用Java Map,通过掌握其高级特性和技巧,让代码更简洁。内容包括Map的初始化、使用Stream API处理Map、利用merge方法、使用ComputeIfAbsent和ComputeIfPresent,以及Map的默认方法。这些技巧不仅提高了代码的可读性和维护性,还提升了开发效率。
606 3
|
人工智能 安全 物联网
智慧农业
智慧农业通过物联网、大数据和AI技术实现精准化、智能化和自动化的农业生产,提升效率、减少浪费并推动可持续发展。其标准体系建设涵盖技术、经营和环境等多个方面,旨在指导农业经营者高效利用智慧农业技术,提升生产和管理效率。尽管面临技术、标准制定及数据安全等挑战,智慧农业仍具有广阔前景,有望引领农业革命,促进产业升级和城乡融合。
|
前端开发 JavaScript 开发者
第一章(概念篇) 微前端介绍与背景
第一章(概念篇) 微前端介绍与背景
566 0