• 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.

    顺序 选择 循环 总结

  • 相关阅读:
    关于java中final变量的小问题
    你在努力工作吗?
    Google Android系统中侵犯Oracle的专利说明
    应用系统之间数据传输的几种方式
    Java内存模型jsr133规范介绍
    程序员40岁之后怎么办?
    eaby技术架构变迁
    缓存使用的一些注意事项
    java对象初始化顺序
    Ubuntu安装Fcitx(小企鹅五笔输入法)
  • 原文地址:https://www.cnblogs.com/Braveliu/p/7363462.html
Copyright © 2020-2023  润新知