抽象工厂模式(大话设计模式)C/C++版本

抽象工厂模式

在这里插入图片描述

C++

参考:https://www.cnblogs.com/Galesaur-wcy/p/15927110.html

#include <iostream>
using namespace std;

// 抽象产品Department ,定义具体产品的公共接口
class Department
{
public:
    virtual ~Department() = default;
    virtual void Insert() = 0;
    virtual Department *GetDepartment() = 0;
};

// 创建具体产品类SqlserDepartment, 定义生产的具体产品;
class SqlserDepartment : public Department
{
public:
    void Insert() override
    {
        cout << "在SQL Server 中给 Department 表增加一条记录!" << endl;
    }
    Department *GetDepartment() override
    {
        cout << "在SQL Server 中根据ID 得到 Department 表一条记录!" << endl;
        return nullptr;
    }
};

// 创建具体产品类AccessDepartment, 定义生产的具体产品;
class AccessDepartment : public Department
{
public:
    void Insert() override
    {
        cout << "在 Access 中给 Department 表增加一条记录!" << endl;
    }
    Department *GetDepartment() override
    {
        cout << "在 Access 中根据ID 得到 Department 表一条记录!" << endl;
        return nullptr;
    }
};

// 抽象产品User ,定义具体产品的公共接口
class User
{
public:
    virtual ~User() = default;
    virtual void Insert() = 0;
    virtual User *GetUser() = 0;
};

// 创建具体产品类SqlserverUser, 定义生产的具体产品;
class SqlserverUser : public User
{
public:
    void Insert() override
    {
        cout << "在SQL Server 中给 User表增加一条记录!" << endl;
    }
    User *GetUser() override
    {
        cout << "在SQL Server 中根据ID 得到 User表一条记录!" << endl;
        return nullptr;
    }
};

// 创建具体产品类AccessUser, 定义生产的具体产品;
class AccessUser : public User
{
public:
    void Insert() override
    {
        cout << "在 Access 中给 User表增加一条记录!" << endl;
    }
    User *GetUser() override
    {
        cout << "在 Access 中根据ID 得到 User表一条记录!" << endl;
        return nullptr;
    }
};

// 抽象工厂,定义具体工厂的公共接口
class AbstractFactory
{
public:
    virtual ~AbstractFactory() = default;
    virtual User *CreateUser() = 0;
    virtual Department *CreateDepartment() = 0;
};

// 创建具体工厂SqlServerFactory,定义创建对应具体产品实例的方法;
class SqlServerFactory : public AbstractFactory
{
public:
    User *CreateUser() override
    {
        return new SqlserverUser(); // 该工厂 生产的产品
    }
    Department *CreateDepartment() override
    {
        return new SqlserDepartment;
    }
};

// 创建具体工厂AccessFactory,定义创建对应具体产品实例的方法;
class AccessFactory : public AbstractFactory
{
public:
    User *CreateUser() override
    {
        return new AccessUser(); // 该工厂 生产的产品
    }
    Department *CreateDepartment() override
    {
        return new AccessDepartment;
    }
};

int main()
{
    // AbstractFactory *factory = new SqlServerFactory;
    AbstractFactory *factory = new AccessFactory;

    // 调用的是 AccessFactory 工厂,准备生产  SqlserverUser 产品
    User *iu = factory->CreateUser();
    iu->Insert();
    iu->GetUser();

    // 调用的是 AccessFactory 工厂,准备生产  AccessDepartment 产品
    Department *id = factory->CreateDepartment();
    id->Insert();
    id->GetDepartment();

    delete factory;
    delete iu;
    delete id;
    factory = nullptr;
    id = nullptr;
    id = nullptr;
    return 0;
}

C

#include <stdio.h>
#include <stdlib.h>

// 抽象产品Department
typedef struct Department
{
    void (*Insert)(void *);
    struct Department *(*GetDepartment)(void *);
} Department;

// 抽象产品User
typedef struct User
{
    void (*Insert)(void *);
    struct User *(*GetUser)(void *);
} User;

// 抽象工厂
typedef struct AbstractFactory
{
    User *(*CreateUser)(void);
    Department *(*CreateDepartment)(void);
} AbstractFactory;

// 具体产品SqlserDepartment
typedef struct SqlserDepartment
{
    Department base;
} SqlserDepartment;

void SqlserDepartment_Insert(void *dept)
{
    printf("在SQL Server 中给 Department 表增加一条记录!\n");
}

Department *SqlserDepartment_GetDepartment(void *dept)
{
    printf("在SQL Server 中根据ID 得到 Department 表一条记录!\n");
    return NULL;
}

// 具体产品AccessDepartment
typedef struct AccessDepartment
{
    Department base;
} AccessDepartment;

void AccessDepartment_Insert(void *dept)
{
    printf("在 Access 中给 Department 表增加一条记录!\n");
}

Department *AccessDepartment_GetDepartment(void *dept)
{
    printf("在 Access 中根据ID 得到 Department 表一条记录!\n");
    return NULL;
}

// 具体产品SqlserverUser
typedef struct SqlserverUser
{
    User base;
} SqlserverUser;

void SqlserverUser_Insert(void *user)
{
    printf("在SQL Server 中给 User表增加一条记录!\n");
}

User *SqlserverUser_GetUser(void *user)
{
    printf("在SQL Server 中根据ID 得到 User表一条记录!\n");
    return NULL;
}

// 具体产品AccessUser
typedef struct AccessUser
{
    User base;
} AccessUser;

void AccessUser_Insert(void *user)
{
    printf("在 Access 中给 User表增加一条记录!\n");
}

User *AccessUser_GetUser(void *user)
{
    printf("在 Access 中根据ID 得到 User表一条记录!\n");
    return NULL;
}

// 具体工厂SqlServerFactory
typedef struct SqlServerFactory
{
    AbstractFactory base;
} SqlServerFactory;

User *SqlServerFactory_CreateUser(void *unused)
{
    SqlserverUser *user = (SqlserverUser *)malloc(sizeof(SqlserverUser));
    user->base.Insert = SqlserverUser_Insert;
    user->base.GetUser = SqlserverUser_GetUser;
    return (User *)user;
}

Department *SqlServerFactory_CreateDepartment(void *unused)
{
    SqlserDepartment *dept = (SqlserDepartment *)malloc(sizeof(SqlserDepartment));
    dept->base.Insert = SqlserDepartment_Insert;
    dept->base.GetDepartment = SqlserDepartment_GetDepartment;
    return (Department *)dept;
}

// 具体工厂AccessFactory
typedef struct AccessFactory
{
    AbstractFactory base;
} AccessFactory;

User *AccessFactory_CreateUser()
{
    AccessUser *user = (AccessUser *)malloc(sizeof(AccessUser));
    user->base.Insert = AccessUser_Insert;
    user->base.GetUser = AccessUser_GetUser;
    return (User *)user;
}

Department *AccessFactory_CreateDepartment()
{
    AccessDepartment *dept = (AccessDepartment *)malloc(sizeof(AccessDepartment));
    dept->base.Insert = AccessDepartment_Insert;
    dept->base.GetDepartment = AccessDepartment_GetDepartment;
    return (Department *)dept;
}

int main()
{
    AbstractFactory *factory = (AbstractFactory *)malloc(sizeof(AccessFactory));
    ((AccessFactory *)factory)->base.CreateUser = AccessFactory_CreateUser;
    ((AccessFactory *)factory)->base.CreateDepartment = AccessFactory_CreateDepartment;

    User *iu = factory->CreateUser();
    iu->Insert(iu);
    iu->GetUser(iu);

    Department *id = factory->CreateDepartment();
    id->Insert(id);
    id->GetDepartment(id);

    free(factory);
    free(iu);
    free(id);

    return 0;
}

总结

在工厂方法模式的基础上,在对应的工厂类中新增了相关产品的创建函数。(其实并不是很方便!!)

扩展:
简单工厂模式似乎更简便点,但是switch部分实在是破坏了开闭原则,可以使用“反射”(即动态识别类型)

C++可以使用静态函数指针数组来实现
参考:https://blog.csdn.net/klarclm/article/details/7352101

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值