06: 抽象工厂模式

news/2024/11/15 19:45:12/文章来源:https://www.cnblogs.com/BoYuCG/p/18416466

1. 案例  在Access和SQL server分别插入User表和Department表

2. 抽象工厂模式结构

 - 抽象产品(AbstractProduct):所有产品的基类,提供产品类的公共方法

struct User
{std::string m_sName = "";uint32_t m_uiID = 0;
};class IUser
{
public:virtual void InsertUser(std::shared_ptr<User> spUser);virtual const std::shared_ptr<User> GetUserByID(uint32_t iID) const;
private:std::unordered_map<uint32_t, std::shared_ptr<User>> m_mapUser;
};
struct Department
{std::string m_sName = "";uint32_t m_uiID = 0;
};class IDepartment
{
public:virtual void InsertDepartment(std::shared_ptr<Department> spDepartment);virtual const std::shared_ptr<Department> GetDepartmentByID(uint32_t iID)const;
private:std::unordered_map<uint32_t, std::shared_ptr<Department>> m_mapDepartment;
};

- 具体产品(ConcreteProduct):具体产品类

class AccessUser :public IUser
{
public:void InsertUser(std::shared_ptr<User> spUser) override;const std::shared_ptr<User> GetUserByID(uint32_t iID) const override;
};void AccessUser::InsertUser(std::shared_ptr<User> spUser)
{std::cout << "在Access中给User表增加了一条记录" << std::endl;IUser::InsertUser(spUser);
}const std::shared_ptr<User> AccessUser::GetUserByID(uint32_t iID) const
{std::cout << "在Access中根据ID得到User表一条记录" << std::endl;return IUser::GetUserByID(iID);
}
class SqlServerUser :public IUser
{
public:void InsertUser(std::shared_ptr<User> spUser) override;const std::shared_ptr<User> GetUserByID(uint32_t iID) const override;
};void SqlServerUser::InsertUser(std::shared_ptr<User> spUser)
{std::cout << "在SQL Server中给User表增加了一条记录:" << std::endl;IUser::InsertUser(spUser);
}const std::shared_ptr<User> SqlServerUser::GetUserByID(uint32_t iID) const
{std::cout << "在SQL Server中根据ID得到User表一条记录" << std::endl;return IUser::GetUserByID(iID);
}
class AccessDepartment :public IDepartment
{
public:void InsertDepartment(std::shared_ptr<Department> spDepartment) override;const std::shared_ptr<Department> GetDepartmentByID(uint32_t iID)const override;
};void AccessDepartment::InsertDepartment(std::shared_ptr<Department> spDepartment)
{std::cout << "在Access中给Department表增加了一条记录" << std::endl;IDepartment::InsertDepartment(spDepartment);
}const std::shared_ptr<Department> AccessDepartment::GetDepartmentByID(uint32_t iID) const
{std::cout << "在Access中根据ID得到Department表一条记录" << std::endl;return IDepartment::GetDepartmentByID(iID);
}
class SqlServerDepartment :public IDepartment
{
public:void InsertDepartment(std::shared_ptr<Department> spDepartment) override;const std::shared_ptr<Department> GetDepartmentByID(uint32_t iID)const override;
};void SqlServerDepartment::InsertDepartment(std::shared_ptr<Department> spDepartment)
{std::cout << "在SQL Server中给Department表增加了一条记录:" << std::endl;IDepartment::InsertDepartment(spDepartment);
}const std::shared_ptr<Department> SqlServerDepartment::GetDepartmentByID(uint32_t iID) const
{std::cout << "在SQL Server中根据ID得到Department表一条记录" << std::endl;return IDepartment::GetDepartmentByID(iID);
}

- 抽象工厂(AbstractFactory): 所有生产具体产品的工厂类的基类,提供工厂类的公共方法

struct IUser;
struct IDepartment;
class Factory
{
public:virtual std::shared_ptr<IUser> CreatUser() = 0;virtual std::shared_ptr<IDepartment> CreatDepartment() = 0;
};

- 具体工厂(ConcreteFactory):生产具体的产品

class AccessFactory :public Factory
{
public:// 通过 Factory 继承std::shared_ptr<IUser> CreatUser() override;std::shared_ptr<IDepartment> CreatDepartment() override;
};std::shared_ptr<IUser> AccessFactory::CreatUser()
{return std::make_shared<AccessUser>();
}std::shared_ptr<IDepartment> AccessFactory::CreatDepartment()
{return std::make_shared<AccessDepartment>();
}
class SqlServerFactory :public Factory
{
public:// 通过 Factory 继承std::shared_ptr<IUser> CreatUser() override;std::shared_ptr<IDepartment> CreatDepartment() override;
};std::shared_ptr<IUser> SqlServerFactory::CreatUser()
{return std::make_shared<SqlServerUser>();
}std::shared_ptr<IDepartment> SqlServerFactory::CreatDepartment()
{return std::make_shared<SqlServerDepartment>();
}

3. 用法

    auto spUser = std::make_shared<User>();spUser->m_sName = "小白";spUser->m_uiID = 1;auto spDepartment = std::make_shared<Department>();spDepartment->m_sName = "小白";spDepartment->m_uiID = 1;{auto spServerFactory = std::make_shared<SqlServerFactory>();auto spServerUser = spServerFactory->CreatUser();auto spServerDepartment = spServerFactory->CreatDepartment();spServerUser->InsertUser(spUser);spServerDepartment->InsertDepartment(spDepartment);auto userGet = spServerUser->GetUserByID(1);std::cout << "客户端获取:ID= " << userGet->m_uiID << " Name=" << userGet->m_sName << std::endl;auto departmentGet = spServerDepartment->GetDepartmentByID(1);std::cout << "客户端获取:ID= " << departmentGet->m_uiID << " Name=" << departmentGet->m_sName << std::endl;}{auto spServerFactory = std::make_shared<AccessFactory>();auto spServerUser = spServerFactory->CreatUser();auto spServerDepartment = spServerFactory->CreatDepartment();spServerUser->InsertUser(spUser);spServerDepartment->InsertDepartment(spDepartment);auto userGet = spServerUser->GetUserByID(1);std::cout << "客户端获取:ID= " << userGet->m_uiID << " Name=" << userGet->m_sName << std::endl;auto departmentGet = spServerDepartment->GetDepartmentByID(1);std::cout << "客户端获取:ID= " << departmentGet->m_uiID << " Name=" << departmentGet->m_sName << std::endl;}

 

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

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

南沙C++信奥老师解一本通题 1371:看病

​【题目描述】有个朋友在医院工作,想请BSNY帮忙做个登记系统。具体是这样的,最近来医院看病的人越来越多了,因此很多人要排队,只有当空闲时放一批病人看病。但医院的排队不同其他排队,因为多数情况下,需要病情严重的人优先看病,所以希望BSNY设计系统时,以病情的严重情…

堪称最优秀的 Docker 可视化管理工具 ——Portainer

Portainer 是一款轻量级的应用,它提供了图形化界面,用于方便地管理 Docker 环境,包括单机环境和集群环境。随着 Docker 内实例越来越多,就得涉及到监控以及统计的需求:有多少个容器?运行的有几个?有哪些容器 CPU 使用率低?... Portainer 是一款轻量级的应用,它提供了图…

分布式数据库中间件:MyCat 和 ShardingSphere

分布式数据库中间件 用于实现 分库、分表、分片、分布式事务、读写分离 等。 本文 是 调查 MyCat 和 ShardingSphere 两款 中间件 的一些信息汇总。本文时间:2024年9月。MyCat Mycat数据库分库分表中间件。ben发布于博客园 http://www.mycat.org.cn github-Mycat1 https://gi…

Python 遭遇 ProxyError 问题记录

本内容复制知乎的一个贴子,在此只做下记录及参考和学习,原链接地址: 最近遇到的一个问题,在搞清楚之后才发现这么多年的 HTTPS_PROXY 都配置错了! 起因 想用 Python 在网上下载一些图片素材,结果 requests 报 requests.exceptions.ProxyError,具体的错误信息见下面。当然…

白云龙期货投资-第二讲

K线图基本用途就是为了寻找“买卖点”,所有的进场点有K线配合能大大的提高进场的成功率。关键点位出现K线及组合配合。大胆进场(波浪理论第五浪。回调黄金分割率点,趋势线,颈线,整数关口等)。K线技术-一切技术之根本 K线图基本用途就是为了寻找“买卖点”,所有的进场点有K…

博客园主题皮肤

背景图:目前使用的是Awescnb主题的geek 参照:https://blog.csdn.net/zk_tww/article/details/141030258

用户验收测试指南0简介

0 简介 这是一本关于多种形式的用户验收测试(UAT)及其用途的。它汇集了有关测试、项目管理、质量管理、团队行为和完整的用户验收测试经验的其他相关材料,并将它们编织成一条牢固可靠的生命线,供用户验收测试新手指南或利益相关者参考。 本书是为满足三类不同人群的需求而编…

以太网PHY芯片详解

以太网PHY芯片详解 什么是phy phy的基本作用 收到MAC过来的数据(PHY没有帧的概念,都是数据而不管什么地址数据还是CRC),进行处理,然后把并行数据转化为串行流数据,再按照物理层的编码规则把数据编码,再变为模拟信号把数据送出去。 实现CSMA/CD(多点接入载波监听/冲突检…

pwnos1

文件泄露 80端口下存在文件读取 尝试读取用户列表 http://192.168.1.110/index1.php?help=true&connect=../../../../../../../etc/passwdroot:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/bin/sh bin:x:2:2:bin:/bin:/bin/sh sys:x:3:3:sys:/dev:/bin/sh…

pwnos2

这台靶机要自行配置攻击机在10.10.10.0/24网段下 sql注入 80端口login处发现可以注入的地方,sqlmap能梭出来很多东西,但是好像都登录不了,--os-shell也是没回显的 sqlmap -u http://10.10.10.100/login.php --data="email=1&pass=1&submit=Login&submitted…

urllib自定义opener对象设置代理IP

urllib.request.urlopen()源代码——urlopen()在干什么返回opener.open(url, data, timeout)方法的结果_opener = None # _opener被赋值为None def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,*, cafile=None, capath=None, cadefault=False, context=N…