3.14 以子类取代类型码
【1】原代码
1 class Employee 2 { 3 public: 4 const static int ENGINEER = 0; 5 const static int SALESMAN = 1; 6 const static int MANAGER = 2; 7 8 public: 9 Employee(); 10 11 int payAmount() 12 { // 典型的switch语句案例。 13 // 这里仅仅列出了一个函数的业务逻辑实现过程,实际中可能会有很多这样的函数,需要进行这样类似的条件判断 14 switch (m_nType) 15 { 16 case ENGINEER: 17 return m_monthlySalary; 18 case SALESMAN: 19 return m_monthlySalary + m_bonus; 20 case MANAGER: 21 return m_monthlySalary + m_commission; 22 default: 23 break; 24 } 25 } 26 27 private: 28 int m_nType; 29 int m_bonus; 30 int m_commission; 31 int m_monthlySalary; 32 };
【2】以子类取代类型码
Employee.h
1 #ifndef _EMPLOYEE_H 2 #define _EMPLOYEE_H 3 4 #include "EmployeeType.h" 5 class Employee 6 { 7 public: 8 Employee(int nType); 9 10 int getType(); 11 void setType(int nType); 12 int payAmount(); 13 14 private: 15 EmployeeType *m_pType; 16 }; 17 #endif
Employee.cpp
1 #include "Employee.h" 2 3 Employee::Employee(int nType) 4 { 5 setType(nType); 6 } 7 8 int Employee::getType() 9 { 10 return m_pType->getTypeCode(); 11 } 12 13 void Employee::setType(int nType) 14 { 15 m_pType = EmployeeType::newType(nType); 16 } 17 18 int Employee::payAmount() 19 { 20 return m_pType->payAmount(); 21 }
EmployeeType.h
1 #ifndef _EMPLOYEETYPE_H 2 #define _EMPLOYEETYPE_H 3 4 class EmployeeType 5 { 6 public: 7 const static int ENGINEER = 0; 8 const static int SALESMAN = 1; 9 const static int MANAGER = 2; 10 11 public: 12 EmployeeType(); 13 static EmployeeType* newType(int code); 14 15 virtual int payAmount() = 0; 16 virtual int getTypeCode() = 0; 17 18 public: 19 int m_bonus; 20 int m_commission; 21 int m_monthlySalary; 22 }; 23 24 #endif
EmpolyeeeType.cpp
1 #include "EmployeeType.h" 2 #include "Engineer.h" 3 #include "Manager.h" 4 #include "Salesman.h" 5 6 EmployeeType::EmployeeType() 7 : m_bonus(1000) 8 , m_commission(500) 9 , m_monthlySalary(2000) 10 {} 11 12 EmployeeType* EmployeeType::newType(int code) 13 { 14 switch (code) 15 { 16 case ENGINEER: 17 return (new Engineer()); 18 case SALESMAN: 19 return (new Salesman()); 20 case MANAGER: 21 return (new Manager()); 22 default: 23 return (new Engineer()); 24 } 25 }
Engineer.h
1 #ifndef _ENGINEER_H 2 #define _ENGINEER_H 3 4 #include "EmployeeType.h" 5 class Engineer : public EmployeeType 6 { 7 public: 8 Engineer(); 9 int getTypeCode(); 10 int payAmount(); 11 }; 12 13 #endif
Engineer.cpp
1 include "Engineer.h" 2 3 Engineer::Engineer() : EmployeeType() 4 {} 5 6 int Engineer::getTypeCode() 7 { 8 return ENGINEER; 9 } 10 11 int Engineer::payAmount() 12 { 13 return m_monthlySalary; 14 }
Manager.h
1 #ifndef _MANAGER_H 2 #define _MANAGER_H 3 4 #include "EmployeeType.h" 5 6 class Manager : public EmployeeType 7 { 8 public: 9 Manager(); 10 int getTypeCode(); 11 int payAmount(); 12 }; 13 14 #endif
Manager.cpp
1 #include "Manager.h" 2 3 Manager::Manager() : EmployeeType() 4 {} 5 6 int Manager::getTypeCode() 7 { 8 return MANAGER; 9 } 10 11 int Manager::payAmount() 12 { 13 return m_monthlySalary + m_commission; 14 }
Salesman.h
1 #ifndef _SALESAMAN_H 2 #define _SALESAMAN_H 3 4 #include "EmployeeType.h" 5 6 class Salesman : public EmployeeType 7 { 8 public: 9 Salesman(); 10 int getTypeCode(); 11 int payAmount(); 12 }; 13 14 #endif
Salesman.cpp
1 #include "Salesman.h" 2 3 Salesman::Salesman() : EmployeeType() 4 {} 5 6 int Salesman::getTypeCode() 7 { 8 return SALESMAN; 9 } 10 11 int Salesman::payAmount() 12 { 13 return m_monthlySalary + m_bonus; 14 }
main.cpp
1 #include "Employee.h" 2 3 void main() 4 { 5 Employee *pEngineer = new Employee(EmployeeType::ENGINEER); 6 pEngineer->payAmount(); 7 }
【3】总结
有一个不可变的类型码,它会影响类的行为。以子类取代这个类型码。
Good Good Study, Day Day Up.
顺序 选择 循环 总结
3.14 以子类取代类型码相关推荐
- Replace Type Code with Subclasses(以子类取代类型码)
有一个不可变的类型码,它会影响类的行为 public class Employee {static final int ENGINNER = 0;static final int SALESMAN = ...
- 代码重构之三种取代类型码(类、子类、状态对象或策略对象)的方式辨析
1.以类取代类型码 适用情况:类之中有一个数值类型码,但它并不影响类的行为. 重构手段:以一个新的类替换该数值类型码. 重构类图示意: 这里的"不影响类的行为"是什么意思呢? 类型 ...
- Replace Type Code with State/Strategy(以State/Strategy取代类型码)
有一个类型码,它会影响类的行为,但你无法通过继承消除它 public class Employee {static final int ENGINNER = 0;static final int SA ...
- 重构——30以类取代类型码(Replace Type Code with Class)
以类取代类型码(Replace Type Code with Class) 类之中有一个数值类型码,但它并不影响类的行为:以一个新的类替换该数值类型码 一.动机 让编译器可以进行类型检查,减少bug ...
- Replace Type Code with Class(以类取代类型码)
类之中有一个数值类型码,但它不影响类的行为 public class Person {public static final int O = 0;public static final int A = ...
- java class获取type_父类通过泛型获得子类Class类型 以及Type体系
正文前先来一波福利推荐: 福利一: 百万年薪架构师视频,该视频可以学到很多东西,是本人花钱买的VIP课程,学习消化了一年,为了支持一下女朋友公众号也方便大家学习,共享给大家. 福利二: 毕业答辩以及工 ...
- 中断系统\中断源及中断分类和中断类型码
文章目录 第八章 中断系统 8.1 中断的基本概念 8.1.1 中断概念的引入及描述 中断方式示意(以输入中断为例) **中断**的定义 8.1.2 中断源及中断分类 中断的分类 8.1.3 中断类型 ...
- 以太网类型码(Ethernet type codes)
以太网类型码(Ethernet type codes) Ethernet Exp. Ethernet Description decimal Hex decimal octal 0000 0000-0 ...
- 中断的分类,中断指令,中断类型码,中断向量表,中断优先级
目录 PC机的中断系统 中断的分类 内部中断(软件中断) 故障 陷阱 异常终止 中断指令 指令类型中断指令 INT N 溢出中断指令 INTO 中断返回指令 IRET 外部中断(硬件中断) 非屏蔽 ...
最新文章
- 4.65FTP服务4.66测试登录FTP
- 2019日历全年一张_python 日历模块calendar
- Linux+varnish安装配置
- html5与css3是互联网发展趋势,五大主流浏览器CSS3和HTML5兼容性比拼
- 高速PCB设计注意事项
- Code::Blocks下载及其汉化教程
- mysql数据库对象是什么意思_数据库对象什么意思
- 安装教程:PostgreSQL + PostGIS + pgAdmin
- 数据库(SQL)中where与having的区别:
- 如果你不甘心CRUD,那需要看这篇设计模式!|原创
- android mp4v2,MP4v2视频库漏洞分析
- HDU - 最大报销额(01背包|贪心)
- python中安装wordcloud 出现cl.exe failed with exit status 2问题解决
- MATLAB处理数据,掌握这几个小技巧就够了
- SSM毕设项目 - 基于SSM的驾校预约管理系统(含源码+论文)
- SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers
- 电子名片如何用一片文章完成拓客引流
- 融智云考计算机,融智云考系统
- uniapp,vue中的身份证格式校验
- 服务器系统日志满了怎么办,服务器事务日志已满解决方法
热门文章
- python培训来袭_从入门到精通!2020年Python最佳学习路线重磅来袭!
- ajax绑值,vue.js使用ajax绑定数据之post方法
- 怀旧服小号最多的服务器,魔兽世界怀旧服小号战场将成为GZS量产高督的基地?...
- php编译成jphp,php编译脚本安装
- git出现红字说明什么_怀孕的第一个月会出现什么变化?若有7种表现,说明可能怀上了...
- php创建实例对象数组,php – 使用arguments数组创建新的对象实例
- ansys怎么使用anand模型_详细剖析ANSYS有限元分析这个软件
- php viewmodel,PHP日记——Lavarel常用语句之View篇
- Spring mvc @ModelAttribute
- 未来新型计算机可能有,上海科学家构建出新型“DNA逻辑门”DNA分子有望成未来超级计算机...