c++抽象工厂方法

本文通过运用设计模式比没用设计模式的优势在哪?

设计模式主要是要抓住稳定部分和易变部分,文章结尾会指出。

数据库操作为例,以下是原有老代码

#include <iostream>
using namespace std;

//数据库连接抽象类
class IDbConnect {
public:
    virtual void connect() = 0;
    virtual ~IDbConnect() = default;
};

//数据库命令抽象类
class IDbCommand {
public:
    virtual void command(IDbConnect *db) = 0;
    virtual ~IDbCommand() = default;
};

//数据库结果抽象类
class IDbGetResult {
public:
    virtual void getResult(IDbCommand *cmd) = 0;
    virtual ~IDbGetResult() = default;
};

//MySql数据库
class MySqlConnect : public IDbConnect{
public:
    void connect() override {
        cout << "mysql connect" << endl;
    }
};

class MySqlCommand : public IDbCommand {
public:
    void command(IDbConnect *db) override {
        cout << "mysql command" << endl;
    }
};

class MySqlGetResult : public IDbGetResult {
public:
    void getResult(IDbCommand *cmd) override {
        cout << "mysql get result" << endl;
    }
};

//MySql数据库
class OracleConnect : public IDbConnect{
public:
    void connect() override {
        cout << "oracle connect" << endl;
    }
};

class OracleCommand : public IDbCommand {
public:
    void command(IDbConnect *db) override {
        cout << "oracle command" << endl;
    }
};

class OracleGetResult : public IDbGetResult {
public:
    void getResult(IDbCommand *cmd) override {
        cout << "oricle get result" << endl;
    }
};

class DbOperator {
public:
    void insertData(void *data) {
        IDbConnect *db = new MySqlConnect;
        db->connect();
        IDbCommand *cmd = new MySqlCommand;
        cmd->command(db);
        IDbGetResult *result = new MySqlGetResult;
        result->getResult(cmd);

        delete(result);
        delete(cmd);
        delete(db);
    }
};

int main()
{
    DbOperator op;
    op.insertData(nullptr);
    return 0;
}

普通工厂方法

#include <iostream>
using namespace std;

//数据库连接抽象类
class IDbConnect {
public:
    virtual void connect() = 0;
    virtual ~IDbConnect() = default;
};

//数据库命令抽象类
class IDbCommand {
public:
    virtual void command(IDbConnect *db) = 0;
    virtual ~IDbCommand() = default;
};

//数据库结果抽象类
class IDbGetResult {
public:
    virtual void getResult(IDbCommand *cmd) = 0;
    virtual ~IDbGetResult() = default;
};


//新增数据库连接工厂
class DbConnectFactory {
public:
    virtual IDbConnect* creatDbConnect() = 0;
    virtual ~DbConnectFactory() = default;
};

//新增数据库命令工厂
class DbCommandFactory {
public:
    virtual IDbCommand* creatDbCommand() = 0;
    virtual ~DbCommandFactory() = default;
};

//新增数据库结果工厂
class DbGetResultFactory {
public:
    virtual IDbGetResult* creatDbGetResult() = 0;
    virtual ~DbGetResultFactory() = default;
};


//MySql数据库
class MySqlConnect : public IDbConnect{
public:
    void connect() override {
        cout << "mysql connect" << endl;
    }
};

class MySqlCommand : public IDbCommand {
public:
    void command(IDbConnect *db) override {
        cout << "mysql command" << endl;
    }
};

class MySqlGetResult : public IDbGetResult {
public:
    void getResult(IDbCommand *cmd) override {
        cout << "mysql get result" << endl;
    }
};


//新增MySql工厂
class MySqlConnectFactory : public DbConnectFactory {
public:
    IDbConnect *creatDbConnect() override {
        return new MySqlConnect;
    }
};

class MySqlCommandFactory : public DbCommandFactory {
public:
    IDbCommand *creatDbCommand() override {
        return new MySqlCommand;
    }
};

class MySqlGetResultFactory : public DbGetResultFactory {
public:
    IDbGetResult *creatDbGetResult() override {
        return new MySqlGetResult;
    }
};


//Oracle数据库
class OracleConnect : public IDbConnect{
public:
    void connect() override {
        cout << "oracle connect" << endl;
    }
};

class OracleCommand : public IDbCommand {
public:
    void command(IDbConnect *db) override {
        cout << "oracle command" << endl;
    }
};

class OracleGetResult : public IDbGetResult {
public:
    void getResult(IDbCommand *cmd) override {
        cout << "oricle get result" << endl;
    }
};


//新增Oracle工厂
class OracleConnectFactory : public DbConnectFactory {
public:
    IDbConnect *creatDbConnect() override {
        return new OracleConnect;
    }
};

class OracleCommandFactory : public DbCommandFactory {
public:
    IDbCommand *creatDbCommand() override {
        return new OracleCommand;
    }
};

class OracleGetResultFactory : public DbGetResultFactory {
public:
    IDbGetResult *creatDbGetResult() override {
        return new OracleGetResult;
    }
};


class DbOperator {
public:
    DbOperator(DbConnectFactory *connectFactory,
               DbCommandFactory *commandFactory,
               DbGetResultFactory *resultFactory) :
               connectFactory(connectFactory),
               commandFactory(commandFactory),
               resultFactory(resultFactory) {}

    void insertData(void *data) {
        IDbConnect *db = connectFactory->creatDbConnect();
        db->connect();
        IDbCommand *cmd = commandFactory->creatDbCommand();
        cmd->command(db);
        IDbGetResult *result = resultFactory->creatDbGetResult();
        result->getResult(cmd);

        delete(result);
        delete(cmd);
        delete(db);
    }

private:
    DbConnectFactory *connectFactory;
    DbCommandFactory *commandFactory;
    DbGetResultFactory *resultFactory;
};

int main()
{
    MySqlConnectFactory connectFactory;
    MySqlCommandFactory commandFactory;
    MySqlGetResultFactory resultFactory;

    DbOperator op(&connectFactory, &commandFactory, &resultFactory);
    op.insertData(nullptr);

    return 0;
}

普通工厂方法有弊端,就是如果无法阻止传入MySqlConnectFactoryOracleCommandFactory

所以抽象工厂出现了,专门解决一系列对象必须同时被使用。

#include <iostream>
using namespace std;

//数据库连接抽象类
class IDbConnect {
public:
    virtual void connect() = 0;
    virtual ~IDbConnect() = default;
};

//数据库命令抽象类
class IDbCommand {
public:
    virtual void command(IDbConnect *db) = 0;
    virtual ~IDbCommand() = default;
};

//数据库结果抽象类
class IDbGetResult {
public:
    virtual void getResult(IDbCommand *cmd) = 0;
    virtual ~IDbGetResult() = default;
};

//抽象工厂
class DbFactory {
public:
    virtual IDbConnect* creatDbConnect() = 0;
    virtual IDbCommand* creatDbCommand() = 0;
    virtual IDbGetResult* creatDbGetResult() = 0;
    virtual ~DbFactory() = default;
};

//MySql数据库
class MySqlConnect : public IDbConnect{
public:
    void connect() override {
        cout << "mysql connect" << endl;
    }
};

class MySqlCommand : public IDbCommand {
public:
    void command(IDbConnect *db) override {
        cout << "mysql command" << endl;
    }
};

class MySqlGetResult : public IDbGetResult {
public:
    void getResult(IDbCommand *cmd) override {
        cout << "mysql get result" << endl;
    }
};


//新增MySql工厂
class MySqlFactory : public DbFactory {
public:
    IDbConnect *creatDbConnect() override {
        return new MySqlConnect;
    }
    IDbCommand *creatDbCommand() override {
        return new MySqlCommand;
    }
    IDbGetResult *creatDbGetResult() override {
        return new MySqlGetResult;
    }

};


//Oracle数据库
class OracleConnect : public IDbConnect{
public:
    void connect() override {
        cout << "oracle connect" << endl;
    }
};

class OracleCommand : public IDbCommand {
public:
    void command(IDbConnect *db) override {
        cout << "oracle command" << endl;
    }
};

class OracleGetResult : public IDbGetResult {
public:
    void getResult(IDbCommand *cmd) override {
        cout << "oricle get result" << endl;
    }
};


//新增Oracle工厂
class OracleFactory : public DbFactory {
public:
    IDbConnect *creatDbConnect() override {
        return new OracleConnect;
    }
    IDbCommand *creatDbCommand() override {
        return new OracleCommand;
    }
    IDbGetResult *creatDbGetResult() override {
        return new OracleGetResult;
    }
};


class DbOperator {
public:
    DbOperator(DbFactory *factory) : factory(factory) {}

    void insertData(void *data) {
        IDbConnect *db = factory->creatDbConnect();
        db->connect();
        IDbCommand *cmd = factory->creatDbCommand();
        cmd->command(db);
        IDbGetResult *result = factory->creatDbGetResult();
        result->getResult(cmd);

        delete(result);
        delete(cmd);
        delete(db);
    }

private:
    DbFactory *factory;
};

int main()
{
    MySqlFactory factory;

    DbOperator op(&factory);
    op.insertData(nullptr);

    return 0;
}

稳定部分是IDbConnect/IDbCommand/IDbGetResult/DbFactory类,抽象部分是它们的子类。

抽象工厂方法添加一系列产品很轻松,比如mongodb或者redis数据库,而在添加操作就很难。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值