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. blogbeta1

    //html <!DOCTYPE html> blog 身高:170 体重:230 座右铭 再给我吃一口 关于我 微信 微博 标签 SM SP 重金求爹 2019/11/16 本人找爹,带 ...

  2. Directx11教程(42) 纹理映射(12)-简单的bump mapping

    原文:Directx11教程(42) 纹理映射(12)-简单的bump mapping        有时候,我们只有一个粗糙的模型,但是我们想渲染纹理细节,比如一个砖墙,我们如何在只有一个平面的时候 ...

  3. PHPCMS快速建站系列之网站迁移(本地到服务器,服务器迁移,更换域名等)

    可能出现的问题: 1.后台登录验证码显示不正常(修改/caches/configs/system.php文件) //网站路径'web_path' => '/', 2.phpsso修改 如果不修改 ...

  4. Kubernetes1.4即将发布

    (一)发布历史 Kubernetes 1.0 - 2015年7月发布 Kubernetes 1.1 - 2015年11月发布 Kubernetes 1.2 - 2016年3月发布 Kubernetes ...

  5. docker search

    命令:docker search [root@iZ943kh74qgZ ~]# docker search --help Usage: docker search [OPTIONS] TERM Sea ...

  6. ubuntu 使用glfw.h 出现函数无法调用

    最近在学习在Ubuntu下使用qt进行opengl开发,使用到了glfw这个库.我安装官网的编译和安装方法进行了配置安装,在usr/local/include的下产生了glfw.h文件. 于是我在我的 ...

  7. Libevent:11使用Libevent的DNS上层和底层功能

    Libevent提供了一些API用来进行DNS域名解析,并且提供了实现简单DNS服务器的能力. 本章首先描述域名解析的上层功能,然后介绍底层功能及服务器功能. 注意:Libevent的当前DNS客户端 ...

  8. 1<<33这种写法是错的!!!

    1<<33不能这么写,1默认int类型,应该改为(long long)1<<33

  9. 给图片加ALT属性是个什么意思?有什么优点呢

    使用百度统计的seo建议当中一条图片ALT信息"存在没有alt信息的img标签"扣分较多.站点图片中加入ALT对seo有什么优点呢?这是非常多初学seo的朋友们都须要询问的问题.a ...

  10. C++类继承中的虚方法

    #include <bits/stdc++.h> using namespace std; class A { public: void Show() { cout << &q ...