多态(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;
}
}
如你所见,我们没有利用已有的继承层次进行计算,而是使用了违反SRP原则的执行方式。要进行重构,我们只需将百分率的计算置于实际的customer类型之中。我知道这只是一项补救措施,但我还是会这么做,就像在代码中那样
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(多态代替条件)的更多相关文章

  1. Replace conditional with Polymorphism

    namespace RefactoringLib.Ploymorphism.Before { public class Customer { } public class Employee : Cus ...

  2. 重构第31天 使用多态替代条件语句( Replace conditional with Polymorphism)

    理解:本文中的”使用多态代替条件判断”是指如果你需要检查对象的类型或者根据类型执行一些操作时,一种很好的办法就是将算法封装到类中,并利用多态性进行抽象调用. 详解:本文展示了面向对象编程的基础之一“多 ...

  3. 重构指南 - 使用多态代替条件判断(Replace conditional with Polymorphism)

    多态(polymorphism)是面向对象的重要特性,简单可理解为:一个接口,多种实现. 当你的代码中存在通过不同的类型执行不同的操作,包含大量if else或者switch语句时,就可以考虑进行重构 ...

  4. 【Java重构系列】重构31式之封装集合

    2009年,Sean Chambers在其博客中发表了31 Days of Refactoring: Useful refactoring techniques you have to know系列文 ...

  5. 编写高质量代码改善C#程序的157个建议——建议104:用多态代替条件语句

    建议104:用多态代替条件语句 假设要开发一个自动驾驶系统.在设计之初,此自动驾驶系统拥有一个驾驶系统命令的枚举类型: enum DriveCommand { Start, Stop } 当前该枚举存 ...

  6. 重构第四天 : 用多态替换条件语句(if else & switch)

    面相对象的一个核心基础就是多态,当你要根据对象类型的不同要做不同的操作的时候,一个好的办法就是采用多态,把算法封装到子类当中去. 重构前代码: public abstract class Custom ...

  7. Overview of Polymorphism -多态的分类

    多态有类型系统衍生. 有限类型.无限类型.确定类型. Classifications Christopher Strachey (1967) introduced the concept of pol ...

  8. 【Java重构系列】重构31式之搬移方法

    重构第二式:搬移方法 (Refactoring 2: Move Method) 毋容置疑,搬移方法(Move Method)应该是最常用的重构手段之一,正因为太常用而且较为简单,以至于很多人并不认为它 ...

  9. polymorphism多态

    [概念] 方法名相同,具体操作根据类不同. eg 有open()方法的ebook, kindle 都会被打开 eg 动物叫声不同 inheritance:只有superclass subclass都有 ...

随机推荐

  1. ip地址的唯一性是如何保证的

    连接ISP网络时,运行商就分配了一个ip地址,所以,ip地址是运营商指定的. 账户只是控制是否可以接入而已,只要是插上网线,就已经动态分配了ip地址.

  2. 设备没有可用空间 /var/spool/clientmqueue sendmail

    [root@hadoop3 /]# crontab -e/tmp/crontab.TB7A7w: 设备上没有空间[root@hadoop3 /]# df -Bg文件系统 1G-块 已用 可用 已用% ...

  3. C#函数3递归

    using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace Console ...

  4. bzoj 1086 王室联邦 —— 思路题

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1086 一眼看去很是不会,于是看看TJ... https://blog.csdn.net/ly ...

  5. 搭建gerrit服务器(apache&nginx反向代理方式)

    这段时间,想搭建一个gerrit,用于代码托管,gerrit的搭建,网上有很多种教程,但是自己按照别人的教程逐步操作,一直出现诸多问题.最头痛的就是:Configuration Error Check ...

  6. hdu2089(数位DP 递推形式)

    不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. 基于dubbo2.5.5+zookeeper3.4.9的服务搭建

    参考资料:https://segmentfault.com/a/1190000009568509https://segmentfault.com/a/1190000004654903 0. 环境 Ja ...

  8. wamp的手动安装

    Wamp的手动安装 (http://www.cnblogs.com/homezzm/archive/2012/08/01/2618062.html) 一.Apache2.4安装 1.修改\Apache ...

  9. mac下安装ngnix以及开启关闭重启

    一.安装 执行如下命令 brew search nginx brew install nginx 安装完以后,可以在终端输出的信息里看到一些配置路径: /usr/local/etc/nginx/ngi ...

  10. 在IDEA里gradle配置和使用

    在IDEA里gradle配置和使用 在IDEA里gradle配置和使用 前言 Windows环境IDEA配置gradle 配置系统环境变量 下载 配置环境变量 测试 idea配置 gradle仓库设置 ...