重构----使用多态代替条件判断
来源:互联网 发布:西南大学网络教育 编辑:程序博客网 时间:2024/06/05 22:32
public abstract class Customer
{
}
public class Employee: Customer
{
}
public class NonEmployee: Customer
{
}
public class OrderProcessor
{
public decimal ProcessOrder(Customercustomer, 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;
}
}
重构为:
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(Customercustomer, IEnumerable<Product> products)
{
// do some processing of order
decimal orderTotal = products.Sum(p => p.Price);
orderTotal -= orderTotal * customer.DiscountPercentage;
return orderTotal;
}
}
- 重构----使用多态代替条件判断
- 使用多态代替条件判断
- 使用条件判断代替异常
- 使用条件判断代替异常
- 31天重构学习笔记31. 使用多态代替条件判断
- 31天重构学习笔记18. 使用条件判断代替异常
- 重构-简化条件语句的使用
- 使用Conditional特性代替#if条件编译
- 6 使用条件判断
- makefile使用条件判断
- Makefile --使用条件判断
- 使用List::unique()代替循环判断
- 使用 EmptyClass 避免条件判断
- Makefile (七) 使用条件判断
- nginx多条件判断
- 条款4:使用Conditional特性代替#if条件编译
- 条款4:使用Conditional特性代替#if条件编译
- Effective C# Item4:使用Conditional特性代替#if条件编译
- jQuery 获得浏览器的宽高
- 亚楠魔域单机V6一键端分享
- android实现顶级圆角弹窗
- Android 平台解析CSV文件
- 设计模式之策略模式(Strategy Pattern)
- 重构----使用多态代替条件判断
- win7多用户远程桌面的方法
- Oracle 11.2.0.3 ORA-12012 ORA-29280 ORA-06512 错误 解决方法
- KNN算法的个人理解
- java中类加载路径和项目根路径获取的几种方式
- struts2 + spring3 + hibernate4 极简平台搭建
- 如何获取SQL Server 2008数据库数据文件的位置
- 恢复数据库时的Assembly错误
- Android实战技巧:如何在ScrollView中嵌套ListView