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

代码示例1

#include <iostream>
#include <fstream>
#include <vector>

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;
    }
  }
}

结果如下

在这里插入图片描述

代码示例2

派生类增加传入指定函数

#include <iostream>
#include <fstream>
#include <vector>
#include <functional>

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;
    }
  }
}

输出结果如下

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/110923.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

区块链物联网中基于属性的私有数据共享与脚本驱动的可编程密文和分散密钥管理

Attribute-Based Private Data Sharing With Script-Driven Programmable Ciphertext and Decentralized Key Management in Blockchain Internet of Things 密钥生成算法 第 1 步&#xff1a;对于属性集A 的用户IDk&#xff0c;他首先将属性集A发送给Pi并且计算 &#xff0c…

亚马逊云科技为奇点云打造全面、安全、可扩展的数据分析解决方案

刘莹奇点云联合创始人、COO&#xff1a;伴随云计算的发展&#xff0c;数据技术也在快速迭代&#xff0c;成为客户迈入DT时代、实现高质量发展的关键引擎。我们很高兴能和云计算领域的领跑者亚马逊云科技一同&#xff0c;不断为客户提供安全可靠的产品与专业的服务。 超过1500家…

项目部署之OpenResty

项目部署之OpenResty 1. OpenResty介绍 OpenResty 是一个基于Nginx的高性能Web平台&#xff0c;用于方便地搭建能够处理超高并发、扩展性极高的动态Web应用、Web服务和动态网关。具备下列特点&#xff1a; 具备Nginx的完整功能基于Lua语言进行扩展&#xff0c;集成了大量精良…

分布式消息队列:Rabbitmq(2)

目录 一:交换机 1:Direct交换机 1.1生产者端代码: 1.2:消费者端代码: 2:Topic主题交换机 2.1:生产者代码: 2.2:消费者代码: 二:核心特性 2.1:消息过期机制 2.1.1:给队列中的全部消息指定过期时间 2.1.2:给某条消息指定过期时间 2.2:死信队列 一:交换机 1:Direct交…

零信任安全模型和多因素身份验证:提升网络安全的关键一步

近年来&#xff0c;随着疫情的蔓延和科技的飞速发展&#xff0c;数据和工作的数字化程度前所未有。这虽然为机会创造提供了更多空间&#xff0c;但也为潜在威胁行为者提供了新的入侵途径。因此&#xff0c;数据泄露的防范已经成为每个组织IT基础设施中不可或缺的一部分。 数据泄…

远程IO在激光行业:实现高效、精准控制的解决方案

激光机简介 激光机是激光雕刻机、激光切割机和激光打标机的总称。激光机利用其高温的工作原理作用于被加工材料表面&#xff0c;同时根据输入到机器内部的图形&#xff0c;绘制出客户要求的图案、文字等。激光机根据用途可分为激光切割机和激光雕刻机。其中&#xff0c;激光切割…

liunx练习题之在同一主机提供多个的web服务

虚拟web主机类型 一、基于端口 1.vim /etc/httpd/conf.d/vhost2.conf ---- — 改变http服务默认访问路径 <directory /testweb1>allowoverride none 表示不允许覆盖其他配置require all granted 表示允许所有请求 </directory> <virtualhost 0.0.0.0:…

NVME CMB原理和常规使用方案详解

什么是CMB 在NVMe Express 1.2 Spec中开始支持一个特性&#xff0c;那就是CMB&#xff08;Controller Memory Buffer&#xff09;&#xff0c;是指SSD控制器内部的读写存储缓冲区&#xff0c;与HMB&#xff08;Host Memory Buffer&#xff09;的不同处在于所使用的内存地址位于…

回归预测 | Matlab实现RIME-CNN-SVM霜冰优化算法优化卷积神经网络-支持向量机的多变量回归预测

回归预测 | Matlab实现RIME-CNN-SVM霜冰优化算法优化卷积神经网络-支持向量机的多变量回归预测 目录 回归预测 | Matlab实现RIME-CNN-SVM霜冰优化算法优化卷积神经网络-支持向量机的多变量回归预测效果一览基本介绍程序设计参考资料 效果一览 基本介绍 1.RIME-CNN-SVM霜冰优化算…

【Linux】深入理解系统文件操作(1w字超详解)

1.系统下的文件操作&#xff1a; ❓是不是只有C\C有文件操作呢&#xff1f;&#x1f4a1;Python、Java、PHP、go也有&#xff0c;他们的文件操作的方法是不一样的啊 1.1对于文件操作的思考&#xff1a; 我们之前就说过了&#xff1a;文件内容属性 针对文件的操作就变成了对…

轧钢测径仪在螺纹钢负公差轧制中的四大作用!

螺纹钢为什么要进行负公差轧制&#xff1f; 在标准允许范围内&#xff0c;越接近负公差&#xff0c;那么在合格规范内&#xff0c;所损耗的原材料越少&#xff0c;而螺纹钢轧制速度快&#xff0c;更是以吨的量进行成交&#xff0c;因此控制的原材料积少成多&#xff0c;对其成本…

因存在色情内容,夸克被罚50万元

媒体经济的繁荣、自媒体、直播等各种形式的信息传播疯狂发展&#xff0c;但是各种形式的信息资源大规模生产时&#xff0c;“色情”&#xff0c;“暴力”的图像和视频不可控的滋生&#xff0c;特别是某些 APP 或浏览器。一旦打开&#xff0c;满屏都是“哥哥&#xff0c;快来啊”…

java判断上午下午的方法

一、代码 import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale;public class Main1 {public static void main(String[] args) {Date nowDate new Date();SimpleDateFormat sdf new SimpleDateFormat("aa", Locale.ENGLISH);Strin…

AI大模型在短视频处理和剪辑中的应用,文末送书

&#x1f3c6;作者简介&#xff0c;黑夜开发者&#xff0c;CSDN领军人物&#xff0c;全栈领域优质创作者✌&#xff0c;CSDN博客专家&#xff0c;阿里云社区专家博主&#xff0c;2023年6月CSDN上海赛道top4。 &#x1f3c6;数年电商行业从业经验&#xff0c;历任核心研发工程师…

矢量图形编辑软件illustrator 2023 mac中文软件特点

illustrator 2023 mac是一款矢量图形编辑软件&#xff0c;用于创建和编辑排版、图标、标志、插图和其他类型的矢量图形。 illustrator 2023 mac软件特点 矢量图形&#xff1a;illustrator创建的图形是矢量图形&#xff0c;可以无限放大而不失真&#xff0c;这与像素图形编辑软…

【Jenkins】新建任务FAQ

问题1. 源码管理处填入Repository URL&#xff0c;报错&#xff1a;无法连接仓库&#xff1a;Error performing git command: ls-remote -h https://github.com/txy2023/GolangLearning.git HEAD 原因&#xff1a; jenkins全局工具配置里默认没有添加git的路径&#xff0c;如果…

全方位 Linux 性能调优经验总结

Part1Linux性能优化 1性能优化 性能指标 高并发和响应快对应着性能优化的两个核心指标&#xff1a;吞吐和延时 图片来自: www.ctq6.cn 应用负载角度&#xff1a;直接影响了产品终端的用户体验系统资源角度&#xff1a;资源使用率、饱和度等 性能问题的本质就是系统资源已经…

【排序算法】 归并排序详解!深入理解!思想+实现!

&#x1f3a5; 屿小夏 &#xff1a; 个人主页 &#x1f525;个人专栏 &#xff1a; 算法—排序篇 &#x1f304; 莫道桑榆晚&#xff0c;为霞尚满天&#xff01; 文章目录 &#x1f4d1;前言&#x1f324;️归并排序的思想☁️基本思想☁️归并的思想实现☁️分治法 &#x1f3…

java 版本企业招标投标管理系统源码+多个行业+tbms+及时准确+全程电子化

项目说明 随着公司的快速发展&#xff0c;企业人员和经营规模不断壮大&#xff0c;公司对内部招采管理的提升提出了更高的要求。在企业里建立一个公平、公开、公正的采购环境&#xff0c;最大限度控制采购成本至关重要。符合国家电子招投标法律法规及相关规范&#xff0c;以及审…

一文彻底理解python浅拷贝和深拷贝

目录 一、必备知识二、基本概念三、列表&#xff0c;元组&#xff0c;集合&#xff0c;字符串&#xff0c;字典浅拷贝3.1 列表3.2 元组3.3 集合3.4 字符串3.5 字典3.6 特别注意可视化展示浅拷贝总结 四、列表&#xff0c;元组&#xff0c;集合&#xff0c;字符串&#xff0c;字…