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 外部中断(硬件中断) 非屏蔽 ...
最新文章
- 李子柒爆红:既然做直播能年薪过亿, 为何还要努力高考?
- 2021年春季学期-信号与系统-第六次作业参考答案-第三小题
- MultipartFile 文件上传
- avro和java原生序列化的区别,java原生序列化和Kryo序列化性能比较
- 磁力链接的BASE32编码向HEX编码的转换
- php ASCII 字符转换 (汉字及特殊字符) 宽字符 (uft8/unicode)
- STM32L562开发板开箱记 STM32L562E-DK Discovery kit电路功能分析
- 图标缩排和悬浮突显的简单实现
- CS229学习笔记(2)多变量线性回归
- 这样去分析大盘才能稳赚不亏best
- 垃圾回收中的finalize方法
- 纽微特记事:有了工作产出,领导吓坏了
- php将请求转发,PHP中实现请求转发(curl)和请求重定向
- 【math】 向量运算:叉乘
- 每日一记—Android动态申请多个权限
- U-SEM体验模型——让游戏交互设计的维度更加清晰
- 自动控制原理(1)-典型环节的传递函数
- ChatGPT4高分通过数据库系统工程师(DBA)认证
- Halo的Sakura主题
- 1706D1 - Chopping Carrots (Easy Version)
热门文章
- 使用sklearn预测走势_Prophet: 时间序列预测库
- android游戏大地图如何加载失败,求助:android studio第一次加载百度地图api失败
- 精通开关电源设计第二版pdf_11、秋招年35月准备期——Verilog HDL高级数字设计(第二版)...
- python sql查询返回记录_干货!Python与MySQL数据库的交互实战
- matlab实现cnn代码,CNN 经典的卷积神经网络MATLAB实现源码,可直接运行。 276万源代码下载- www.pudn.com...
- java lang runtimeexception什么意思_Java基础之异常
- 2.13 向量化 logistic 回归
- pandas 散布矩阵
- flask-sqlalchemy 数据基本操作--实例
- 信息安全完全参考手册之本书框架(目录)