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. QLabel添加Click信号

    使用自定义label来实现此功能 其他控件可参照此例. #include "customerqlabel.h" CustomerQlabel::CustomerQlabel(QWi ...

  2. 【JZOJ4930】【NOIP2017提高组模拟12.18】C

    题目描述 给出一个H的行和W列的网格.第i行第j列的状态是由一个字母的A[i][j]表示,如下: "." 此格为空. "o" 此格包含一个机器人. " ...

  3. 公司电脑安装mysql出现小问题

    按步骤将mysql安装好后,在自己电脑完全没问题,但是在公司电脑安装的时候出现了这样的问题. 查阅资料以后,找到了问题: 参考链接:https://blog.csdn.net/huacode/arti ...

  4. 关于JSP的淘汰问题(转)

    来源:http://1t.click/peD 大中型公司需要专业人才,小公司需要全才,但是对于个人职业发展来说,我建议是分开.你要是这辈子就吃java这碗饭,就不要去研究什么css,js等等. 把你的 ...

  5. Leetcode811.Subdomain Visit Count子域名访问计数

    一个网站域名,如"discuss.leetcode.com",包含了多个子域名.作为顶级域名,常用的有"com",下一级则有"leetcode.com ...

  6. 用python爬虫抓站的一些技巧总结 zz

    用python爬虫抓站的一些技巧总结 zz 学用python也有3个多月了,用得最多的还是各类爬虫脚本:写过抓代理本机验证的脚本,写过在discuz论坛中自动登录自动发贴的脚本,写过自动收邮件的脚本, ...

  7. Java练习 SDUT-3849_分数四则运算

    分数四则运算 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 编写程序,实现两个分数的加减法 Input 输入包含多行数 ...

  8. mysql 中合并查询结果union用法 or、in与union all 的查询效率

    mysql 中合并查询结果union用法 or.in与union all 的查询效率 (2016-05-09 11:18:23) 转载▼ 标签: mysql union or in 分类: mysql ...

  9. Java练习 SDUT-2746_大小写转换

    大小写转换 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description X现在要学习英文以及各种稀奇古怪的字符的了.现在他想把一串字 ...

  10. Linux 用户he用户组管理

    8)系统中有一类用户称为伪用户(psuedo users). 这些用户在/etc/passwd  文件中也占有一条记录,但是不能登陆,因为他们的登陆shell 为空,他们的存在主要是方便系统管理,满足 ...