多态(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. MVC 下 JsonResult 的使用方法(JsonRequestBehavior.AllowGet)<转>

    MVC 默认 Request 方式为 Post. actionpublic JsonResult GetPersonInfo(){var person = new{Name = "张三&qu ...

  2. Java类加载机制?

    深入研究Java类加载机制 类加载是Java程序运行的第一步,研究类的加载有助于了解JVM执行过程,并指导开发者采取更有效的措施配合程序执行. 研究类加载机制的第二个目的是让程序能动态的控制类加载,比 ...

  3. HashMap的数据机构是什么样的?

    参考:http://www.cnblogs.com/ITtangtang/p/3948406.html

  4. hexSHA1散列加密解密(不可逆)

    1.maven引入codec和commons依赖: <dependency> <groupId>commons-codec</groupId> <artifa ...

  5. silverlight子窗体操作数据库后刷新父窗体

    silverlight子窗体操作数据库后刷新父窗体 作者 Kant 写于 2011 年 07 月 02 日 分类目录 学习笔记, 所有文章 C# Silverlight 代码 刷新 学习 异步刷新 数 ...

  6. Ural2040:Palindromes and Super Abilities(离线&manecher算法)

    Dima adds letters s1, …, sn one by one to the end of a word. After each letter, he asks Misha to tel ...

  7. 《Perceptual Losses for Real-Time Style Transfer and Super-Resolution》论文笔记

    参考 http://blog.csdn.net/u011534057/article/details/55052304 代码 https://github.com/yusuketomoto/chain ...

  8. iOS编码的一些规范

    1.实例变量instance variable,最好带上前缀下划线,例如 @interface  Person:NSObject { NSString *_name; int *_age; } @en ...

  9. (转)IE内存泄露,iframe内存泄露造成的原因和解决方案

    http://my.oschina.net/jsan/blog/11169 http://blog.csdn.net/tianma630/article/details/8502395 jQuery ...

  10. LuoguP1606 [USACO07FEB]荷叶塘Lilypad Pond 【最短路】By cellur925

    最短路好题!] 参考资料:学长 https://blog.csdn.net/TSOI_Vergil/article/details/52975779 学长太强了!!!%%% 题目传送门 ======= ...