The solution to duplicated code involves twe steps(Extraction and Invocation) that would be explained in detail in subsequent section.

Extraction means that programmer need to extract the duplicated code into a method defined in a class. You can add a new class or modify existing class to include the method. If you are sure that the method is invoked within only one class, it shoud be defined as private method. otherwise, the access specifier of the method should be public or others.

Invocation just means invoking the method described above. Invocation contains direct invocation and indirect invocation.
Direct invocation looks like this:
class caller {
callerMethod { callee.calleeMethod() }
}
The method invoked directly is typically used as utility method.

The way of indirect invocation contains Factory Pattern, IoC/DI and AOP. The method invoked indirectly by Factory Pattern or IoC/DI is generally used as shared business logic. Considering the case: many places in program need to call a method, but some places need to keep the logic and some places need to change the logic. In this case, Directly invocation would have problem with it. Because the methed invocation expression in caller methods are still duplicated. the solution to this case is to introduce a interface to replace the concrete implementation, then use Factory Pattern to generate the concrete instance or use IoC/DI to inject the instance. thus we can get away with changing caller method. For Factory Pattern caller need to depend on factory, while IoC/DI don't have such problem.

The method invoked indirectly by AOP is generally used as base/system service. Considering the case: the system service cross the business logic or some place need to remove the system service. In this case, AOP is the best solution.

The solution to duplicated code的更多相关文章

  1. IntelliJ IDEA “Finds duplicated code”提示如何关闭

    发现重复的代码这个提示真的很烦啊,我们怎么关闭他呢. 设置在这里: Settings -> Editor -> Inspections -> General -> Duplic ...

  2. IDEA SQL dialect detection和Duplicated Code检测关闭

    IDEA似乎做的太多,对于Mybatis文件中的SQL语法检查可能就没有太大的必要性,Duplicated Code检测其实非常好,但是我测试使用JDBC代码的时候一堆波浪线让我很不舒服 因此将这两个 ...

  3. 关闭Found duplicated code

    IDEA中的这个“发现重复代码 - Found duplicated code“的这个提示甚是烦躁. Settings —> Editor —> Inspections —> Gen ...

  4. [Python Test] Use pytest fixtures to reduce duplicated code across unit tests

    In this lesson, you will learn how to implement pytest fixtures. Many unit tests have the same resou ...

  5. BookNote: Refactoring - Improving the Design of Existing Code

    BookNote: Refactoring - Improving the Design of Existing Code From "Refactoring - Improving the ...

  6. ResolveUrl in ASP.NET - The Perfect Solution

    原文:ResolveUrl in ASP.NET - The Perfect Solution If you are looking for ResolveUrl outside of Page/Co ...

  7. Visualize Code with Visual Studio

    In this post, App Dev Manager Ed Tovsen spotlight the features and benefits of Code Maps in Visual S ...

  8. PMD -- An extensible cross-language static code analyzer.

    PMD An extensible cross-language static code analyzer. https://github.com/pmd/pmd 跨语言静态代码分析工具.可以查找通用 ...

  9. IDEA提示 found duplicate code

    原因: IntelliJ IDEA提示Found duplicated code in this file 这不是我们代码错误,而是idea提示说我们的代码有重复,在项目的其他地方有同样的代码片段 解 ...

随机推荐

  1. 廖雪峰Python总结2

    1.切片 L[0:3]表示,从索引0开始,直到索引3为止,但是不包括索引3.如果第一个索引是0,还可以省略L[:3] 倒数切片:L[-n:-1],-1是倒数第一个元素,L[-n:-1]不包括倒数第一个 ...

  2. GNN 相关资料记录;GCN 与 graph embedding 相关调研;社区发现算法相关;异构信息网络相关;

    最近做了一些和gnn相关的工作,经常听到GCN 和 embedding 相关技术,感觉很是困惑,所以写下此博客,对相关知识进行索引和记录: 参考链接: https://www.toutiao.com/ ...

  3. day4-转自金角大王

    Python之路,Day4   本节大纲 迭代器&生成器 装饰器  基本装饰器 多参数装饰器 递归 算法基础:二分查找.二维数组转换 正则表达式 常用模块学习 作业:计算器开发 实现加减乘除及 ...

  4. Dubbo报org.I0Itec.zkclient.exception.ZkNoNodeException异常

    解决办法就是添加zkclient的jar,maven工程的话增加如下引用: <dependency>     <groupId>com.github.sgroschupf< ...

  5. 【JZOJ4893】【NOIP2016提高A组集训第15场11.14】过河

    题目描述 数据范围 解法 由于同一个点,同一个圆盘最多只会走一次. 把(i,j)当作一个点,表示第i个点,放第i个圆盘. 那么就可以使用最短路. 时间复杂度为O(n4∗k). 事实上存在冗余圆盘,一个 ...

  6. Unity3D 物体旋转之Quaternion.Slerp

    实现的功能:1个物体以一定的速度转向目标物体 Quaternion TargetRotation = Quaternion.LookRotation(m_Target.transform.positi ...

  7. CoreData遇见iCloud的那些坑

    尽管苹果把iCloud与CoreData之间的完美配合吹的天花乱坠,但在iOS7之前,想用iCloud同步CoreData数据简直就是噩梦,苹果自己也承认了之前的诸多bug和不稳定性,这让苹果不得不重 ...

  8. 分享 KubeCon 2019 (上海)关于 Serverless 及 Knative 相关演讲会议

    有幸参加了 KubeCon 2019 上海大会,并参加了 Knative 及 Serverless 相关的几场分享会,收获满满.这里简单介绍一下各个演讲主题的主要内容.详细的演讲主题文档可以在Kube ...

  9. MySql计算两日期时间之间相差的天数,秒数,分钟数,周数,小时数

    MySql计算两日期时间之间相差的天数,秒数,分钟数,周数,小时数 计算两日期时间之间相差的天数,秒数,分钟数,周数,小时数,这里主要分享的是通过MySql内置的函数 TimeStampDiff() ...

  10. @总结 - 2@ 位运算卷积/子集卷积 —— FWT/FMT

    目录 @0 - 参考资料@ @1 - 异或卷积概念及性质@ @2 - 快速沃尔什正变换(异或)@ @3 - 快速沃尔什逆变换(异或)@ @4 - 与卷积.或卷积@ @5 - 参考代码实现@ @6 - ...