首页 > 其他分享 >06: 抽象工厂模式

06: 抽象工厂模式

时间:2024-09-16 17:34:43浏览次数:8  
标签:std 06 cout 工厂 public 抽象 shared ptr const

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

 

标签:std,06,cout,工厂,public,抽象,shared,ptr,const
From: https://www.cnblogs.com/BoYuCG/p/18416466

相关文章

  • 05: 工厂方法模式
    1. 案例:加减乘除运算2.工厂方法模式结构 - 抽象工厂(AbstractFactory):所有生产具体产品的工厂类的基类,提供工厂类的公共方法template<typenameT>classOperationFactory{public:virtualstd::shared_ptr<Operation<T>>CreatOperation()=0;};-具体工......
  • 简单工厂模式
    1.案例:营业员根据客户所购买商品的单价和数量,根据不同活动向客户收费-正常原价收费-八折收费-满300返1002.简单工厂模式结构-抽象产品(AbstractProduct):具体产品类的基类,包含创建产品的公共方法enumCashType{NORMAL=0,REBATE,RETURN};class......
  • 06_Python数据类型_元组
    Python的基础数据类型数值类型:整数、浮点数、复数、布尔字符串容器类型:列表、元祖、字典、集合元组元组(Tuple)是一种不可变的序列类型,与列表类似,但有一些关键的区别。本质:只读的列表(list)定义元组元组是由圆括号()包围的元素集合,元素之间用逗号分隔。元组一旦创建,其内......
  • 【题解】【动态规划】—— [NOIP2006 普及组] 开心的金明
    【题解】【动态规划】——[NOIP2006普及组]开心的金明[NOIP2006普及组]开心的金明题目描述输入格式输出格式输入输出样例输入#1输出#1提示1.题意解析2.AC代码2.1.二维d......
  • P3067 [USACO12OPEN] Balanced Cow Subsets G
    我的天,折半搜索(meetinthemiddle),依稀记得我学过,但是真的不记得。。。。从状态图上起点和终点同时开始进行宽度/深度优先搜索,如果发现相遇了,那么可以认为是获得了可行解。这道题,每一个元素会有3种状态,分别是在第一个集合或者第二个集合亦或者不在集合中。如果直接暴力去搜的......
  • Java抽象类和接口的学习了解
    目录1.抽象类1.1抽象类概念1.2例子1.3 抽象类语法1.被abstract修饰的类--抽象类2.抽象类中被abstract修饰的方法--抽象方法,该方法不用给出具体的实现体3.当一个类中含有抽象方法时,该类必须要abstract修饰4.抽象类也是类,内部可以包含普通方法和属性,甚至构造方法......
  • 「KDOI-06-S」题解
    T2树上异或题面分析树形DP题考虑一颗子树内部的某种割边方式,假设其被分为\(n\)个连通块,每个连通块的权值分别为\(a_1,a_2,\dots,a_n\),那么该子树在这种割边方式下对答案的贡献就为\(\prod_{i=1}^{n}a_i\)。因此就可以从叶子向根不断合并,求出每种割边状态的值,时......
  • 2024-06-02 矩阵重塑2
    include<bits/stdc++.h>usingnamespacestd;constintmaxn=1e4+1;intmar[maxn];voidtmar(intmar[],constintn,constintm){intmat[n+1][m+1],mat1[m+1][n+1];inti,j;for(i=1;i<=n;i++){for(j=1;j<=m;j++){mat[i][j]=mar[(i-1)*m+j];}}for(......
  • 洛谷P1006
    题目传送门:传送门p1006题目描述小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题。一次素质拓展活动中,班上同学安排坐成一个 mm 行 nn 列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法直接交谈了。幸运的是,他们可以通过传纸条来进行交流。纸条要......
  • D06【python接口自动化学习】-python基础
    day06注释学习日期:20240913学习目标:注释:如何写程序的说明书?学习笔记:1.1 如何编写注释注释的位置注释写在代码上面,是最常用的形式注释写在代码前面,常用于代码调试注释的内容怎么写注释要解释代码是做什么,以下建议注释2,不采用注释1python之禅总结注释......