多态(polymorphism)是面向对象的重要特性,简单可理解为:一个接口,多种实现。

当你的代码中存在通过不同的类型执行不同的操作,包含大量if else或者switch语句时,就可以考虑进行重构,将方法封装到类中,并通过多态进行调用。

代码重构前:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LosTechies.DaysOfRefactoring.SampleCode.BreakMethod.After; namespace LosTechies.DaysOfRefactoring.SampleCode.ReplaceWithPolymorphism.Before
{
public abstract class Customer
{
} public class Employee : Customer
{
} public class NonEmployee : Customer
{
} public class OrderProcessor
{
public decimal ProcessOrder(Customer customer, IEnumerable<Product> products)
{
// do some processing of order
decimal orderTotal = products.Sum(p => p.Price); Type customerType = customer.GetType();
if (customerType == typeof(Employee))
{
orderTotal -= orderTotal * 0.15m;
}
else if (customerType == typeof(NonEmployee))
{
orderTotal -= orderTotal * 0.05m;
} return orderTotal;
}
}
}

代码重构后:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LosTechies.DaysOfRefactoring.SampleCode.BreakMethod.After; namespace LosTechies.DaysOfRefactoring.SampleCode.ReplaceWithPolymorphism.After
{
public abstract class Customer
{
public abstract decimal DiscountPercentage { get; }
} public class Employee : Customer
{
public override decimal DiscountPercentage
{
get { return 0.15m; }
}
} public class NonEmployee : Customer
{
public override decimal DiscountPercentage
{
get { return 0.05m; }
}
} public class OrderProcessor
{
public decimal ProcessOrder(Customer customer, IEnumerable<Product> products)
{
// do some processing of order
decimal orderTotal = products.Sum(p => p.Price); orderTotal -= orderTotal * customer.DiscountPercentage; return orderTotal;
}
}
}

重构后的代码,将变化点封装在了子类中,代码的可读性和可扩展性大大提高。

重构指南 - 使用多态代替条件判断(Replace conditional with Polymorphism)的更多相关文章

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

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

  2. 一次项目代码重构-使用spring容器干掉条件判断

    一次项目代码重构-使用spring容器干掉条件判断 这是在一次公司项目中进行重构时,一些复杂业务时想到的一个去掉一些if else的办法.能够使代码逻辑更加清晰,减少一些业务上的耦合. 业务说明 我所 ...

  3. Replace conditional with Polymorphism

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

  4. Spring Framework 条件装配 之 @Conditional

    Spring Framework 条件装配 之 @Conditional 前言 了解SpringBoot的小伙伴对Conditional注解一定不会陌生,在SpringBoot项目中,Conditio ...

  5. 重构第18天 用条件语句来代替异常(Replace exception with conditional)

    理解:本文中的“使用条件判断代替异常”是指把没有必要使用异常做判断的条件尽量改为条件判断. 详解: 重构前代码: public class Microwave { private IMicrowave ...

  6. 重构指南 - 封装条件(Encapsulate Conditional)

    封装就是将相关的方法或者属性抽象成为一个对象. 封装的意义: 对外隐藏内部实现,接口不变,内部实现自由修改. 只返回需要的数据和方法. 提供一种方式防止数据被修改. 更好的代码复用.   当代码中包含 ...

  7. 一行代码调用实现带字段选取+条件判断+排序+分页功能的增强ORM框架

    问题:3行代码 PDF.NET是一个开源的数据开发框架,它的特点是简单.轻量.快速,易上手,而且是一个注释完善的国产开发框架,受到不少朋友的欢迎,也在我们公司的项目中多次使用.但是,PDF.NET比起 ...

  8. oracle触发器加条件判断

    oracle触发器加条件判断,如果某个字段,isnode=0,那么不执行下面的方法,数据如下: create or replace trigger tr_basestation_insert_emp ...

  9. 重构第16天 封装条件(Encapsulate Conditional)

    理解:本文中的“封装条件”是指条件关系比较复杂时,代码的可读性会比较差,所以这时我们应当根据条件表达式是否需要参数将条件表达式提取成可读性更好的属性或者方法,如果条件表达式不需要参数则可以提取成属性, ...

随机推荐

  1. MySQL索引的索引长度问题

    转自:http://samyubw.blog.51cto.com/978243/223773 MySQL的每个单表中所创建的索引长度是有限制的,且对不同存储引擎下的表有不同的限制. 在MyISAM表中 ...

  2. Python登陆人人网

    #!coding:utf-8 import urllib2 import urllib import cookielib def renrenBrower(url,user,password): #登 ...

  3. centos安装mysql57

    下载源安装文件 https://dev.mysql.com/downloads/repo/yum/ wget http://repo.mysql.com//mysql57-community-rele ...

  4. 高版本sketch文件转成低版本的sketch

    https://pan.baidu.com/s/1htmNERU 下载 该文件然后在放到高版本sketch文件的目录下,执行下面命令 chmod +x ./build.sh ./build.sh 文件 ...

  5. CentOS7.4关闭防火墙

    //临时关闭 systemctl stop firewalld //禁止开机启动 systemctl disable firewalld Removed symlink /etc/systemd/sy ...

  6. 22.Container With Most Water(能装最多水的容器)

    Level:   Medium 题目描述: Given n non-negative integers a1, a2, ..., an , where each represents a point ...

  7. StyleSheet

    StyleSheet.create()方法 //定义组件 var App = React.createClass({ render:function () { return( <View sty ...

  8. Qt 学习之路 2(50):自定义可编辑模型

    Home / Qt 学习之路 2 / Qt 学习之路 2(50):自定义可编辑模型 Qt 学习之路 2(50):自定义可编辑模型 豆子 2013年5月13日 Qt 学习之路 2 13条评论 上一章我们 ...

  9. Trailing Loves (or L'oeufs?)

    The number "zero" is called "love" (or "l'oeuf" to be precise, literal ...

  10. Python-is, ==, cmp()

    is 主要是判断 2 个变量是否引用的是同一个对象,如果是的话,则返回 true,否则返回 false. 判断数字相等不要用 is 操作符 1 2 3 4 5 6 7 8 9 10 11 12 > ...