• c++ 继承方式高内聚read write function操作


    代码示例1

    #include 
    #include 
    #include 
    
    using namespace std;
    
    struct BaseDevice
    {
      BaseDevice(const std::string sType, const std::string sApplication) : strType(sType), strApplication(sApplication)
      {}
    
      virtual ~BaseDevice();
    
    
      std::string strType;
      std::string strApplication;
    };
    BaseDevice::~BaseDevice() {}
    
    struct GetDeviceInfor : public BaseDevice
    {
      GetDeviceInfor(const std::string sType, const std::string sApplication) : BaseDevice(sType, sApplication)
      {}
    
      virtual ~GetDeviceInfor();
    };
    GetDeviceInfor::~GetDeviceInfor()
    {}
    
    template<class T1>
    struct SetDeviceInfor : public BaseDevice
    {
      SetDeviceInfor(const std::string sType, const std::string sApplication, T1 iValues):BaseDevice(sType, sApplication), iValues(iValues)
      {}
    
      ~SetDeviceInfor() 
      {}
    
      T1 iValues;
    };
    
    
    int main(int argc, char** argv)
    {
      std::vector<std::shared_ptr<BaseDevice>> vecDeviceInfor = {
      std::make_shared<GetDeviceInfor>("TemputerData", "FirstScreen"),
      std::make_shared<GetDeviceInfor>("RealValue", "MeasurmentMode"),
      std::make_shared<SetDeviceInfor<std::string>>("MachineValue", "MaintainerMode", "45")
      };
    
      for (auto pDeviceInfor : vecDeviceInfor)
      {
        if (pDeviceInfor)
        {
          std::shared_ptr<GetDeviceInfor> pGetDeviceTemoInfor = std::dynamic_pointer_cast<GetDeviceInfor>(pDeviceInfor);
          if (pGetDeviceTemoInfor != nullptr)
          {
            std::cout << pGetDeviceTemoInfor->strApplication << std::endl;
          }
    
          std::shared_ptr<SetDeviceInfor<std::string>> pSetDeviceTemoInfor = std::dynamic_pointer_cast<SetDeviceInfor<std::string>>(pDeviceInfor);
          if (pSetDeviceTemoInfor != nullptr)
          {
            std::cout << pSetDeviceTemoInfor->strApplication << std::endl;
            std::cout << pSetDeviceTemoInfor->iValues << std::endl;
          }
        }
        else
        {
          std::cout << "empty container!" << std::endl;
        }
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74

    结果如下

    在这里插入图片描述

    代码示例2

    派生类增加传入指定函数

    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    
    class StorageClass
    {
    public:
      StorageClass();
      ~StorageClass();
      void setData(int iData)
      {
        iOperateData = iData;
      }
      void setStringData(std::string sData)
      {
        std::cout << "setData:" << sData << std::endl;
        strData = sData;
      }
    
      int getData()
      {
        return iOperateData;
      }
      std::string getStringData()
      {
        return strData;
      }
    
    private:
      int iOperateData = 1;
      std::string strData;
    };
    
    StorageClass::StorageClass()
    {
    
    }
    
    StorageClass::~StorageClass()
    {
    
    }
    
    struct BaseDevice
    {
      BaseDevice(const std::string sType, const std::string sApplication) : strType(sType), strApplication(sApplication)
      {}
    
      virtual ~BaseDevice();
    
    
      std::string strType;
      std::string strApplication;
    };
    BaseDevice::~BaseDevice() {}
    
    struct GetDeviceInfor : public BaseDevice
    {
      GetDeviceInfor(std::function<int(std::unique_ptr<StorageClass>&)> cFunction, const std::string sType, const std::string sApplication) : BaseDevice(sType, sApplication), cFunction(cFunction)
      {}
    
      virtual ~GetDeviceInfor();
      std::function<int(std::unique_ptr<StorageClass>&)> cFunction;
    };
    GetDeviceInfor::~GetDeviceInfor()
    {}
    
    template<class T1>
    struct SetDeviceInfor : public BaseDevice
    {
      SetDeviceInfor(std::function<void(std::unique_ptr<StorageClass>&, T1) > cFunction, const std::string sType, const std::string sApplication, T1 iValues):
        BaseDevice(sType, sApplication), 
        iValues(iValues), 
        cFunction(cFunction)
      {}
    
      ~SetDeviceInfor() 
      {}
    
      T1 iValues;
      std::function<void(std::unique_ptr<StorageClass>&, T1) > cFunction;
    };
    
    
    int main(int argc, char** argv)
    {
      std::vector<std::shared_ptr<BaseDevice>> vecDeviceInfor = {
      std::make_shared<GetDeviceInfor>(&StorageClass::getData, "TemputerData", "FirstScreen"),
      std::make_shared<GetDeviceInfor>(&StorageClass::getData, "RealValue", "MeasurmentMode"),
      std::make_shared<SetDeviceInfor<std::string>>(&StorageClass::setStringData, "MachineValue", "MaintainerMode", "45")
      };
    
      for (auto pDeviceInfor : vecDeviceInfor)
      {
        if (pDeviceInfor)
        {
          std::shared_ptr<GetDeviceInfor> pGetDeviceTemoInfor = std::dynamic_pointer_cast<GetDeviceInfor>(pDeviceInfor);
          if (pGetDeviceTemoInfor != nullptr)
          {
            std::cout << pGetDeviceTemoInfor->strApplication << std::endl;
    
            std::unique_ptr<StorageClass> puniDevice = std::make_unique<StorageClass>();
            int rValue = pGetDeviceTemoInfor->cFunction(puniDevice);
            std::cout << "rValue:" << rValue << std::endl;
    
          }
    
          std::shared_ptr<SetDeviceInfor<std::string>> pSetDeviceTemoInfor = std::dynamic_pointer_cast<SetDeviceInfor<std::string>>(pDeviceInfor);
          if (pSetDeviceTemoInfor != nullptr)
          {
            std::cout << pSetDeviceTemoInfor->strApplication << std::endl;
            std::cout << pSetDeviceTemoInfor->iValues << std::endl;
    
            std::unique_ptr<StorageClass> puniDevice = std::make_unique<StorageClass>();
            pSetDeviceTemoInfor->cFunction(puniDevice, "123");
            std::cout << "GetData:" << puniDevice->getStringData() << std::endl;
          }
        }
        else
        {
          std::cout << "empty container!" << std::endl;
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127

    输出结果如下

    在这里插入图片描述

  • 相关阅读:
    读《vue3设计与实现》1
    媲美iptables的Windows端口转发——netsh
    玩机搞机---卸载内置软件 无root权限卸载不需要的软件 安全卸载
    MAC 版PowerPoint 插入latex数学公式
    offset新探索:双管齐下,加速大数据量查询
    关于工信部发布的app备案以及小程序备案流程
    android翻转效果时钟
    数字孪生技术在工业制造领域的研究与实践
    前端总结——计算机网络
    苹果ios打包签名ipa文件应用app的验证的证书是怎么授信的原理是什么?
  • 原文地址:https://blog.csdn.net/qq_32348883/article/details/134054467