The solution to duplicated code
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的更多相关文章
- IntelliJ IDEA “Finds duplicated code”提示如何关闭
发现重复的代码这个提示真的很烦啊,我们怎么关闭他呢. 设置在这里: Settings -> Editor -> Inspections -> General -> Duplic ...
- IDEA SQL dialect detection和Duplicated Code检测关闭
IDEA似乎做的太多,对于Mybatis文件中的SQL语法检查可能就没有太大的必要性,Duplicated Code检测其实非常好,但是我测试使用JDBC代码的时候一堆波浪线让我很不舒服 因此将这两个 ...
- 关闭Found duplicated code
IDEA中的这个“发现重复代码 - Found duplicated code“的这个提示甚是烦躁. Settings —> Editor —> Inspections —> Gen ...
- [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 ...
- BookNote: Refactoring - Improving the Design of Existing Code
BookNote: Refactoring - Improving the Design of Existing Code From "Refactoring - Improving the ...
- ResolveUrl in ASP.NET - The Perfect Solution
原文:ResolveUrl in ASP.NET - The Perfect Solution If you are looking for ResolveUrl outside of Page/Co ...
- Visualize Code with Visual Studio
In this post, App Dev Manager Ed Tovsen spotlight the features and benefits of Code Maps in Visual S ...
- PMD -- An extensible cross-language static code analyzer.
PMD An extensible cross-language static code analyzer. https://github.com/pmd/pmd 跨语言静态代码分析工具.可以查找通用 ...
- IDEA提示 found duplicate code
原因: IntelliJ IDEA提示Found duplicated code in this file 这不是我们代码错误,而是idea提示说我们的代码有重复,在项目的其他地方有同样的代码片段 解 ...
随机推荐
- JS原生实现五角星评价详情demo
<style> *{margin: 0;padding: 0;} .pingfen{ width: 195px; margin:10px auto; height:20px; positi ...
- 【JZOJ4923】【NOIP2017提高组模拟12.17】巧克力狂欢
题目描述 Alice和Bob有一棵树(无根.无向),在第i个点上有ai个巧克力.首先,两人个选择一个起点(不同的),获得点上的巧克力:接着两人轮流操作(Alice先),操作的定义是:在树上找一个两人都 ...
- linux 查看并关闭窗口
查看端口 ps -ef | grep tomcat 关闭端口 kill -9 端口号
- 小爬爬7:回顾&&crawlSpider
1.回顾昨日内容 回顾 - 全站数据爬取(分页) - 手动请求的发送Request(url,callback) - post请求和cookie处理 - start_requests(self) - F ...
- hdu4325 线段树 成段更新
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #def ...
- SDUT-2138_判断可达性
数据结构实验之图论三:判断可达性 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 在古老的魔兽传说中,有两个军团,一个叫 ...
- Java练习 SDUT-4303_简单的复数运算(类和对象)
简单的复数运算(类和对象) Time Limit: 2000 ms Memory Limit: 65536 KiB Problem Description 设计一个类Complex,用于封装对复数的下 ...
- 【NS2】新协议的添加示例(转载)
1. 下表显示了 NS2 和 TCP/IP.OSI七层网络结构的大致对应关系(这个表很有好处哦) TCP NS2 OSI 应用层 应用层 应用层 表示层 会话 ...
- java基础部分的一些有意思的东西。
${li.key!=''&&li.key!= null}可以直接判断不为空 ${empty li.value}也是不为空. 最近好烦迭代map里的map或者map里的list 后来发现 ...
- 类的重载case2
/*功能:类的成员属性和成员方法*/ public class Person{ public static void main(String args[]){ TruePerson ...