重构31-Replace conditional with Polymorphism(多态代替条件)
多态(Polymorphism)是面向对象编程的基本概念之一。在这里,是指在进行类型检查和执行某些类型操作时,最好将算法封装在类中,并且使用多态来对代码中的调用进行抽象。
public class OrderProcessor {
public Double ProcessOrder(Customer customer, List<Product> products) {
// do some processing of order
Double orderTotal = sum(products);
Type customerType = customer.GetType();
if (customerType == typeof(Employee)) {
orderTotal -= orderTotal * 0.15d;
} else if (customerType == typeof(NonEmployee)) {
orderTotal -= orderTotal * 0.05d;
}
return orderTotal;
}
}
public abstract class Customer {
}
public class Employee extends Customer {
}
public class NonEmployee extends Customer {
}
public abstract class Customer {
public abstract Double DiscountPercentage(){
return null;
}
}
public class Employee extends Customer {
@Override
public Double DiscountPercentage(){
return 0.15d;
}
}
public class NonEmployee extends Customer {
@Override
public Double DiscountPercentage(){
return 0.05d;
}
}
public class OrderProcessor {
public Double ProcessOrder(Customer customer, List<Product> products) { // do some processing of order
Double orderTotal = sum(products);
orderTotal -= orderTotal * customer.DiscountPercentage();
return orderTotal;
}
}
重构31-Replace conditional with Polymorphism(多态代替条件)的更多相关文章
- Replace conditional with Polymorphism
namespace RefactoringLib.Ploymorphism.Before { public class Customer { } public class Employee : Cus ...
- 重构第31天 使用多态替代条件语句( Replace conditional with Polymorphism)
理解:本文中的”使用多态代替条件判断”是指如果你需要检查对象的类型或者根据类型执行一些操作时,一种很好的办法就是将算法封装到类中,并利用多态性进行抽象调用. 详解:本文展示了面向对象编程的基础之一“多 ...
- 重构指南 - 使用多态代替条件判断(Replace conditional with Polymorphism)
多态(polymorphism)是面向对象的重要特性,简单可理解为:一个接口,多种实现. 当你的代码中存在通过不同的类型执行不同的操作,包含大量if else或者switch语句时,就可以考虑进行重构 ...
- 【Java重构系列】重构31式之封装集合
2009年,Sean Chambers在其博客中发表了31 Days of Refactoring: Useful refactoring techniques you have to know系列文 ...
- 编写高质量代码改善C#程序的157个建议——建议104:用多态代替条件语句
建议104:用多态代替条件语句 假设要开发一个自动驾驶系统.在设计之初,此自动驾驶系统拥有一个驾驶系统命令的枚举类型: enum DriveCommand { Start, Stop } 当前该枚举存 ...
- 重构第四天 : 用多态替换条件语句(if else & switch)
面相对象的一个核心基础就是多态,当你要根据对象类型的不同要做不同的操作的时候,一个好的办法就是采用多态,把算法封装到子类当中去. 重构前代码: public abstract class Custom ...
- Overview of Polymorphism -多态的分类
多态有类型系统衍生. 有限类型.无限类型.确定类型. Classifications Christopher Strachey (1967) introduced the concept of pol ...
- 【Java重构系列】重构31式之搬移方法
重构第二式:搬移方法 (Refactoring 2: Move Method) 毋容置疑,搬移方法(Move Method)应该是最常用的重构手段之一,正因为太常用而且较为简单,以至于很多人并不认为它 ...
- polymorphism多态
[概念] 方法名相同,具体操作根据类不同. eg 有open()方法的ebook, kindle 都会被打开 eg 动物叫声不同 inheritance:只有superclass subclass都有 ...
随机推荐
- XMU 1071 圣斗士黄金十二宫(七)银河星爆 【计算几何】
1071: 圣斗士黄金十二宫(七)银河星爆 Time Limit: 500 MS Memory Limit: 64 MBSubmit: 193 Solved: 10[Submit][Status] ...
- POJ3189 Steady Cow Assignment —— 二分图多重匹配/最大流 + 二分
题目链接:https://vjudge.net/problem/POJ-3189 Steady Cow Assignment Time Limit: 1000MS Memory Limit: 65 ...
- YTU 2953: A代码填充--学画画
2953: A代码填充--学画画 时间限制: 1 Sec 内存限制: 128 MB 提交: 62 解决: 52 题目描述 最近小平迷上了画画,经过琨姐的指导,他学会了RGB色彩的混合方法.对于两种 ...
- JSP 与 ACTION 之间的跳转
<script language="javascript">function delconfirm(url){ if(confirm("你确定要删除本条数据吗 ...
- pandas 学习 —— 逻辑表达式与布尔索引
>> df = pd.DataFrame(np.random.randint(0, 10, (5, 4)), columns=list('ABCD')) A B C D 0 0 4 8 4 ...
- 【POJ 1947】 Rebuilding Roads
[题目链接] 点击打开链接 [算法] f[i][j]表示以i为根的子树中,最少删多少条边可以组成j个节点的子树 树上背包,即可 [代码] #include <algorithm> #inc ...
- AutoIT: 对Windows桌面的一些操作
$handle= WinGetHandle("Program Manager") $ctrl= ControlGetHandle("ProgramManager" ...
- bzoj 1014 火星人prefix —— splay+hash
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1014 用 splay 维护字符串上不同位置的哈希值还是第一次... 具体就是每个节点作为位置 ...
- hdu 2829(四边形优化 && 枚举最后一个放炸弹的地方)
Lawrence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 去除inline-block的间隙
产生间隙的原因就是标签之间的空格,去除的方法: 1 设置父元素的font-size:0;空格字符的宽高都为0, <div class="demo1 demo2"> &l ...