抽象工厂模式
抽象工厂模式和工厂模式不同的是:创建一系列相互依赖的对象先上代码:
#pragma once
#include<string>
#include<iostream>
using namespace std;
class IDBConnection {
public:
virtual void ConnectionString(string& str) = 0;
};
class IDBCommand {
public:
virtual void CommandText(const string& str) = 0;
virtual void setConnection(IDBConnection* connection) = 0;
};
class IDataReader {
public:
virtual void read(IDBCommand* command) = 0;
};
//支持Sql
class SqlConnection :public IDBConnection {
public:
void ConnectionString(string& str) override {
cout << "Sql database Connection...." << endl;
}
};
class SqlCommand :public IDBCommand {
public:
void CommandText(const string& str) override{
cout << "Sql Command text Setting..." << endl;
}
void setConnection(IDBConnection* connection) override {
cout << "Sql Connection Setting Connection!!!" << endl;
}
};
class SqlDataReader :public IDataReader {
void read(IDBCommand* command) override {
cout << "Sql start read data!" << endl;
}
};
//支持Sql
class OracleConnection :public IDBConnection {
public:
void ConnectionString(string& str) override {
cout << "Oracle database Connection...." << endl;
}
};
class OracleCommand :public IDBCommand {
public:
void CommandText(const string& str) override {
cout << "Oracle Command text Setting..." << endl;
}
void setConnection(IDBConnection* connection) override {
cout << "Oracle Connection Setting Connection!!!" << endl;
}
};
class OracleDataReader :public IDataReader {
void read(IDBCommand* command) override {
cout << "Oracle start read data!" << endl;
}
};
class IDBFactory {
public:
virtual IDBConnection* CreatDBConnection() = 0;
virtual IDBCommand* CreatDBCommand() = 0;
virtual IDataReader* CreatDataRead() = 0;
virtual ~IDBFactory() {}
};
class OracleDBFactory: public IDBFactory{
public:
IDBConnection* CreatDBConnection() override {
return new OracleConnection();
}
IDBCommand* CreatDBCommand() override {
return new OracleCommand();
}
virtual IDataReader* CreatDataRead() override{
return new OracleDataReader();
}
};
class SqlDBFactory : public IDBFactory {
public:
IDBConnection* CreatDBConnection() override {
return new SqlConnection();
}
IDBCommand* CreatDBCommand() {
return new SqlCommand();
}
virtual IDataReader* CreatDataRead() {
return new SqlDataReader();
}
};
以数据库的连接为例,需要多个相互依赖的类工作协作,完成连接并读取数据库的数据。在文件AbstractFactory.h 中,IDBConnection,IDBCommand,IDataReader就是三个相互依赖的类,将他们都设为抽象基类,方便后续的SQL、Oracle、MySql等数据库的扩展。
抽象类工厂基类IDBFactory用于创建上述三个相互依赖的类,每个数据库类型都需要创建这三个类。
测试程序如下:
#include<iostream>
#include"AbstractFactory.h"
using namespace std;
class EmployDAO {
private:
IDBFactory* dbfactory;
public:
EmployDAO(IDBFactory* dbfactory) {
this->dbfactory = dbfactory;
}
void DataBaseConnect()
{
string str = { "abc,123456" };
IDBConnection* connection = dbfactory->CreatDBConnection();
connection->ConnectionString(str);
IDBCommand* command = dbfactory->CreatDBCommand();
command->CommandText(static_cast<string>("abc,123456"));
command->setConnection(connection);//关联
IDataReader* read = dbfactory->CreatDataRead();
read->read(command);//关联
}
};
int main() {
IDBFactory* dbfactory1 = new SqlDBFactory();
EmployDAO* emp1 = new EmployDAO(dbfactory1);
emp1->DataBaseConnect();
cout << "===========" << endl;
IDBFactory* dbfactory2 = new OracleDBFactory();
EmployDAO* emp2 = new EmployDAO(dbfactory2);
emp2->DataBaseConnect();
return 0;
}
}
通过这种方式使得EmployDAO的方法DataBaseConnect在需求变化时,无需更改,只需要在文件AbstractFactory.h中添加对应的具体类和具体类对应的工厂类即可。
运行结果如下:
要点:
只能应对“多系列对象构建”的需求变化,即相互依赖的多个对象构建,单个对象的需求变得则无法适应。