9.6 replace conditional with polymorphism (以多态取代条件表达式)
来源:互联网 发布:cda数据分析师招聘 编辑:程序博客网 时间:2024/04/29 23:02
条件表达式,根据对象类型的不同而选择不同的行为。
将这个条件表达式的每一个分支放进一个子类内的覆写函数中,然后将原始函数声明为抽象函数。
动机:
多态使你不必编写明显的条件表达式。
如果同一组条件表达式在程序许多地方出现,那么使用多态的收益是最大的。
做法:
使用replace conditional with polymorphism之前,首先必须有一个继承结构。
如果要处理的条件表达式是一个更大函数中的一部分,首先对条件表达式进行分析,然后使用extract method。
如果有必要,使用move method将条件表达式放置到继承结构的顶端。
任选一个子类,在其中建立一个函数,使之覆写超类中容纳条件表达式的那个函数。将与该子类相关的条件表达式分支复制到新建函数中,并调整。
旧代码
class Engineer extends EmployeeType{ int getTypeCode(){ return Employee.ENGINEER; }}class Manager extends EmployeeType{ int getTypeCode(){ return Employee.MANAGER; }}class Salesman extends EmployeeType{ int getTypeCode(){ return Employee.SALEMAN; }}class Employee...private employeeType _type;int payAmount(){ switch(getType()){ case EmployeeType.ENGINEER: return _monthlySalary; case EmployeeType.SALESMAN: return _monthlySalary + _commission; case EmployeeType.MANAGER: return _monthlySalary + bonus; default; throw new RuntimeException(); }}int getType(){ return _type.getTypeCode();}void setType(int arg){ _type = EmployeeType.newType(arg);}class employeeType...static EmployeeType newType(int code){ switch(code){ case ENGINEER: return new Engineer(); case SALESMAN: return new Salesman(); case MANAGER: return new Manager(); default: throw new IllegalArgumentException(); }}static final int ENGINEER = 0;static final int SALESMAN = 1;static final int MANAGER = 2;
新代码
class Employee...private Engineer _type;int payAmount(){ return _type.payAmount(this);}class Engineer...int payAmount(Employee emp){ return emp.getMonthlySalary();}class Salesman...int payAmount(Employee emp){ return emp.getMonthlySalary() + emp.getCommission();}class Manager...int payAmount(Employee emp){ return emp.getMonthlySalary() + emp.getBonus();}class EmployeeType...abstract int payAmount(Employee emp);
- 9.6 replace conditional with polymorphism (以多态取代条件表达式)
- 简化条件表达式之六 :Replace Conditional with Polymorphism(以多态取代条件式)
- Replace Conditional with Polymorphism(以多态取代条件式)
- Replace Conditional with Polymorphism
- 简化条件表达式之五 :Replace Nested Conditional with Guard Clauses(以卫语句取代嵌套条件式)
- Refactoring Day 31 : Replace conditional with Polymorphism
- Replace Nested Conditional with Guard Clauses(以卫语句取代嵌套条件式)
- Replace Nested Conditional with Guard Clauses(以卫语句取代嵌套条件式)
- 重构——以多态取代条件表达式(状态模式)
- Replace Temp With Query(以查询取代临时变量)
- Replace Method with Method Object(以函数对象取代函数)
- Replace Data Value with Object(以对象取代数据值)
- Replace Array with Object(以对象取代数组)
- Replace Record with Data Class(以数据类取代记录 )
- Replace Type Code with Class(以类取代型别码)
- Replace Type Code with Subclasses(以子类取代型别码)
- Replace Subclass with Fields(以值域取代子类)
- 10.8 replace parameter with methods(以函数取代参数)
- 今年苹果研发所占收入的比例史上最低
- 自动登录 java实现 网络编程
- 用RealPlayer控件制作的播放器
- 11-10-18 练练手,不然就生了
- 记录: Jsoup小应用
- 9.6 replace conditional with polymorphism (以多态取代条件表达式)
- 实现单链表的对称判定算法
- showmount无法使用
- pl/sql
- 常见的 NSString 和 NSMutableString 方法
- ASP.NET跨页面传值技巧总结
- 正义战胜邪恶
- 包含arxHeader时定义CMESH引发的错误
- HTML5 LocalStorage 本地存储