重构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都有 ...
随机推荐
- solr 命令
本文为转载内容:源地址:http://blog.csdn.net/matthewei6/article/details/50620600 查看帮助 bin/solr -help ...
- ZOJ3261 Connections in Galaxy War —— 反向并查集
题目链接:https://vjudge.net/problem/ZOJ-3261 In order to strengthen the defense ability, many stars in g ...
- 什么是以太坊私钥储存(Keystore)文件
进入keystore管理以太坊私钥的障碍很大,主要是因为以太坊客户端在直接的命令行或图形界面下隐藏了大部分的密码复杂性. 例如,用geth: $ geth account new Your new a ...
- 并不对劲的bzoj4651:loj2086:uoj222:p1712:[NOI2016]区间
题目大意 有\(n\)(\(n\leq 5*10^5\))个闭区间\([L_1,R_1],[L_2,R_2],...,[L_n,R_n]\)(\(\forall i\in [1,n],0\leq L_ ...
- Spring Bean状态(转)
Spring-beans的核心实体是BeanDefinition和BeanFactory.前者映射我们的定义,后者则是依据定义生产bean的工厂. 上图是spring beans的静态结构图,更多是 ...
- UI:用UITableView制作通讯录的关键代码
分析{功能分析(打电话.添加联系人.修改联系人),模块分析(联系人展示.详情模块.添加模块)} 拿到一个项目,首先分析项目框架(工程框架) 首先:判断是否是用户第一次安装:(如果是的,那就加载用户引导 ...
- 搭建CARDBOARD+ANDROID+unity3d的VR开发环境
一.下载最新unity3d(u3d官网) 二.下载最新cardboardsdkforunity(https://github.com/googlesamples/cardboard-unity) 三. ...
- bzoj 4552: [Tjoi2016&Heoi2016]排序【二分+线段树】
二分值mid,然后把>=mid的赋值为1,其他赋值为0,每次排序就是算出区间内01的个数,然后分别把0和1放到连续的一段内,这些都可以用线段树来维护 二分的判断条件是操作完之后q位置上是否为1 ...
- bzoj 4521: [Cqoi2016]手机号码【数位dp】
比较基础的数位dp,dfs的时候带上上一位,上上位,是否已经有连续3个相同位,是否有4,是否有8即可 但是要注意两点(在洛谷上一直70) 当l=1e10的时候,直接输出clc(r)即可,因为如果再减去 ...
- bzoj 3105: [cqoi2013]新Nim游戏【线性基+贪心】
nim游戏的先手必胜条件是所有堆的火柴个数异或和为0,也就是找一个剩下火柴堆数没有异或和为0的子集的方案,且这个方案保证剩下的火柴个数总和最大 然后我就不会了,其实我到现在也不知道拟阵是个什么玩意-- ...