三、抽象工厂模式实例

1、数据库实例

wKioL1nM-OizZX97AAFVbGsgNBI521.jpg


数据库User表:
#ifndef USER_H
#define USER_H
#include <string>
using std::string;
 
//数据库User表
class User
{
public:
    unsigned int  getID()
    {
        return m_ID;
    }
    void setID(unsigned int n)
    {
        m_ID = n;
    }
    string getName()
    {
        return m_name;
    }
    void setName(string name)
    {
        m_name = name;
    }
private:
    unsigned int m_ID;
    string m_name;
};
 
#endif // USER_H
数据库Department表:
#ifndef DEPARTMENT_H
#define DEPARTMENT_H
#include <string>
using std::string;
 
//数据库Department表
class Department
{
public:
    unsigned int  getID()
    {
        return m_ID;
    }
    void setID(unsigned int n)
    {
        m_ID = n;
    }
    string getName()
    {
        return m_name;
    }
    void setName(string name)
    {
        m_name = name;
    }
private:
    unsigned int m_ID;
    string m_name;
 
};
 
#endif // DEPARTMENT_H
IFactory接口定义一个创建访问User表和Department表对象的抽象工厂接口。
抽象工厂IFactory:
#ifndef IFACTORY_H
#define IFACTORY_H
 
class IUser;
class IDepartment;
//抽象工厂类
class IFactory
{
public:
    virtual IUser* createUser() = 0;
    virtual IDepartment* createDepartment() = 0;
};
 
#endif // IFACTORY_H
MySQLFactory类实现IFactory接口,实现MySQLUser和MySQLDepartment。
具体工厂MySQLFactory:
#ifndef MYSQLFACTORY_H
#define MYSQLFACTORY_H
#include "IFactory.h"
#include "MySQLUser.h"
#include "MySQLDepartment.h"
 
//具体工厂类
class MySQLFactory : public IFactory
{
public:
    virtual IUser* createUser()
    {
        IUser* user = new MySQLUser();
        return user;
    }
    virtual IDepartment* createDepartment()
    {
        IDepartment* department= new MySQLDepartment();
        return department;
    }
};
 
#endif // MYSQLFACTORY_H
SQLServerFactory类实现IFactory接口,实现SQLServerUser和SQLServerDepartment。
具体工厂SQLServerFactory:
#ifndef SQLSERVERFACTORY_H
#define SQLSERVERFACTORY_H
#include "IFactory.h"
#include "SQLServerUser.h"
#include "SQLServerDepartment.h"
 
//具体工厂类
class SQLServerFactory : public IFactory
{
public:
    virtual IUser* createUser()
    {
        IUser* user = new SQLServerUser();
        return user;
    }
    virtual IDepartment* createDepartment()
    {
        IDepartment* department= new SQLServerDepartment();
        return department;
    }
};
 
#endif // SQLSERVERFACTORY_H
 
抽象产品IUser:
#ifndef IUSER_H
#define IUSER_H
 
class User;
//表User抽象产品类
class IUser
{
public:
    virtual void insertUser(User* user) = 0;
    virtual User* getUser(unsigned int id) = 0;
};
 
#endif // IUSER_H
具体产品MySQLUser:
#ifndef MYSQLUSER_H
#define MYSQLUSER_H
#include "IUser.h"
#include <iostream>
using std::cout;
using std::endl;
 
//User具体产品类
class MySQLUser : public IUser
{
public:
    virtual void insertUser(User* user)
    {
        cout << "insert a record to User table on MySQL" << endl;
    }
    virtual User* getUser(unsigned int id)
    {
        cout << "get a record by ID from User table on MySQL" << endl;
        return NULL;
    }
};
 
#endif // MYSQLUSER_H
具体产品SQLServerUser:
#ifndef SQLSERVERUSER_H
#define SQLSERVERUSER_H
#include "IUser.h"
#include <iostream>
using std::cout;
using std::endl;
 
//User具体产品类
class SQLServerUser : public IUser
{
public:
    virtual void insertUser(User* user)
    {
        cout << "insert a record to User table on SQLServer" << endl;
    }
    virtual User* getUser(unsigned int id)
    {
        cout << "get a record by ID from User table on SQLServer" << endl;
        return NULL;
    }
};
 
#endif // SQLSERVERUSER_H
IDepartment接口用于客户端访问,解除与具体数据库访问的耦合。
抽象产品IDepartment:
#ifndef IDEPARTMENT_H
#define IDEPARTMENT_H
 
class Department;
//表Department抽象产品类
class IDepartment
{
public:
    virtual void insertDepartment(Department* department) = 0;
    virtual Department* getDepartment(unsigned int id) = 0;
};
 
#endif // IDEPARTMENT_H
MySQLDepartment类用于访问MySQL的Department类。
 
具体产品MySQLDepartment:
#ifndef MYSQLDEPARTMENT_H
#define MYSQLDEPARTMENT_H
#include "IDepartment.h"
#include <iostream>
using std::cout;
using std::endl;
 
//Department具体产品类
class MySQLDepartment : public IDepartment
{
public:
    virtual void insertDepartment(Department* department)
    {
        cout << "insert a record to Department Table on MySQL" << endl;
    }
    virtual Department* getDepartment(unsigned int id)
    {
        cout << "get a record by ID from Department table on MySQL" << endl;
        return NULL;
    }
};
 
#endif // MYSQLDEPARTMENT_H
SQLServerDepartment类用于访问SQLServer的Department类。
具体产品SQLServerDepartment:
#ifndef SQLSERVERDEPARTMENT_H
#define SQLSERVERDEPARTMENT_H
#include "IDepartment.h"
#include <iostream>
using std::cout;
using std::endl;
 
//Department具体产品类
class SQLServerDepartment : public IDepartment
{
public:
    virtual void insertDepartment(Department* department)
    {
        cout << "insert a Department to SQLServer" << endl;
    }
    virtual Department* getDepartment(unsigned int id)
    {
        cout << "get a Department by ID from SQLServer" << endl;
        return NULL;
    }
};
 
#endif // SQLSERVERDEPARTMENT_H
客户调用程序:
#include <iostream>
#include "User.h"
#include "Department.h"
#include "IFactory.h"
#include "IUser.h"
#include "IDepartment.h"
#include "MySQLFactory.h"
#include "SQLServerFactory.h"
 
using namespace std;
 
int main()
{
    User user;
    Department department;
 
    IFactory* factoryMySQL = new MySQLFactory();
    IUser* iuMySQL = factoryMySQL->createUser();
    iuMySQL->insertUser(&user);
    iuMySQL->getUser(2);
 
    IDepartment* idMySQL = factoryMySQL->createDepartment();
    idMySQL->insertDepartment(&department);
    idMySQL->getDepartment(1);
 
    IFactory* factorySQLServer = new SQLServerFactory();
    IUser* iuSQLServer = factorySQLServer->createUser();
    iuSQLServer->insertUser(&user);
    iuSQLServer->getUser(2);
 
    IDepartment* idSQLServer = factorySQLServer->createDepartment();
    idSQLServer->insertDepartment(&department);
    idSQLServer->getDepartment(1);
 
    delete factoryMySQL;
    delete iuMySQL;
    delete idMySQL;
    delete factorySQLServer;
    delete iuSQLServer;
    delete idSQLServer;
 
    return 0;
}