用=======隔开的是两处代码重复的地方,
第一个表示同一个类自身的代码重复CentralControlInfoImpl.java自身的代码有重复,该类的128-153(26行)和172-197(26行)行的代码完全重复,重复的代码可以抽出来,写成一个公用方法
第二个表示两个不同的类代码重复ClientDelayRecordServiceImpl.java的136-263(128行)ClientMetricsRecordServiceImpl.java的103-230(128行)的代码重复,提示重复的代码可以抽出来,写一个工具类
Refactoring duplicates
Once you have located some duplicates, several refactoring strategies may apply depending of the scope and extent of the duplication. Here’s a quick summary:
- If the duplication is local to a method or single class:
Extract a local variable if the duplicated logic is not prohibitively long
Extract the duplicated logic into a private method
- If the duplication occurs in siblings within a class hierarchy:
Extract a method and pull it up in the class hierarchy, along with common fields
Use the Template Method design pattern
- If the duplication occurs consistently in unrelated hierarchies:
Introduce a common ancestor to those class hierarchies
重构重复的代码:
一旦您找到了一些重复代码,可能会根据重复代码的范围和程度应用多种重构策略。以下是快速总结:
-
如果重复代码属于方法或单个类:
如果重复代码逻辑不长,提取局部变量
将重复代码逻辑提取为一种私有方法
-
如果重复代码发生在类层次结构中的同级中:
提取方法并在类层次结构中将其与常见字段一起上拉
使用模板方法设计模式
-
如果重复代码在无关的组织存档中一致发生:
将共同祖先介绍到这些类的层次结构中
新手和高级读者可能想要在“重构大师”上阅读更深入的策略、用例和解释。