目录
1、抽象工厂模式的提出
2、抽象工厂模式的定义
3、需求描述
4、抽象工厂方法模式实现
在软件功能设计中,经常面临着“一系列相互依赖的对象”的创建工作,同时,由于需求的变化,这些相互依赖的对象会有更多子类的创建功能。可以将创建对象的new封装在尽量少的的文件中,避免使用者直接依赖子类的变化,降低代码间的耦合关系。
抽象工厂模式主要是解决“一系列对象对象的创建工作,且这些对象有相互依赖或者作用的关系”。抽象工厂模式可以将一系列的变化对象分别创建对应的抽象基类,然后将这些有相互作用的抽象基类的对象创建集成到一个工厂类中。
创建mysql、MongoDb数据库的连接、命令执行和阅读数据的接口,并且数据库可以进行扩展。
工厂方法模式实现
#include
namespace AbsfactModel{
//数据库访问相关操作
class DataConnection
{
public:
DataConnection(){};
virtual ~DataConnection(){};
};
class DataConnectionFactory
{
public:
virtual DataConnection* createDataConnection()=0;
};
class DataCommand
{
public:
DataCommand(){};
virtual ~DataCommand(){};
};
class DataCommandFactory
{
public:
virtual DataCommand* createDataCommand()=0;
};
class DataReader
{
public:
DataReader(){};
virtual ~DataReader(){};
};
class DataReaderFactory
{
public:
virtual DataReader* createDataDataReader()=0;
};
//MySQL
class MySqlConnection:public DataConnection
{
public:
MySqlConnection()
{
std::cout << "MySqlConnection " << std::endl;
}
~MySqlConnection()
{
std::cout << "~MySqlConnection " << std::endl;
}
};
class MySqlConnectionFactory:public DataConnectionFactory
{
public:
virtual DataConnection* createDataConnection()override
{
return new MySqlConnection();
};
};
class MySqlCommand:public DataCommand
{
public:
MySqlCommand()
{
std::cout << "MySqlCommand " << std::endl;
}
~MySqlCommand()
{
std::cout << "~MySqlCommand " << std::endl;
}
};
class MySqlCommandFactory:public DataCommandFactory
{
public:
virtual DataCommand* createDataCommand()override
{
return new MySqlCommand();
};
};
class MySqlDataReader:public DataReader
{
public:
MySqlDataReader()
{
std::cout << "MySqlDataReader " << std::endl;
}
~MySqlDataReader()
{
std::cout << "~MySqlDataReader " << std::endl;
}
};
class MySqlDataReaderFactory:public DataReaderFactory
{
public:
virtual DataReader * createDataDataReader() override
{
return new MySqlDataReader();
}
};
//MongoDb
class MongoDbConnection:public DataConnection
{
public:
MongoDbConnection()
{
std::cout << "MongoDbConnection " << std::endl;
}
~MongoDbConnection()
{
std::cout << "~MongoDbConnection " << std::endl;
}
};
class MongoDbConnectionFactory:public DataConnectionFactory
{
public:
virtual DataConnection * createDataConnection() override
{
return new MongoDbConnection();
}
};
class MongoDbCommand:public DataCommand
{
public:
MongoDbCommand()
{
std::cout << "MongoDbCommand " << std::endl;
}
~MongoDbCommand()
{
std::cout << "~MongoDbCommand " << std::endl;
}
};
class MongoDbCommandFactory:public DataCommandFactory
{
public:
virtual DataCommand * createDataCommand() override
{
return new MongoDbCommand();
}
};
class MongoDbDataReader:public DataReader
{
public:
MongoDbDataReader()
{
std::cout << "MongoDbDataReader " << std::endl;
}
~MongoDbDataReader()
{
std::cout << "~MongoDbDataReader " << std::endl;
}
};
class MongoDbDataReaderFactory:public DataReaderFactory
{
public:
virtual DataReader * createDataDataReader() override
{
return new MongoDbDataReader();
}
};
}
class UseData
{
public:
UseData(AbsfactModel::DataConnectionFactory* cnFt,
AbsfactModel::DataCommandFactory* cdFt,
AbsfactModel::DataReaderFactory* rdFt):
connectionFact(cnFt),commandFact(cdFt),readerFact(rdFt)
{};
AbsfactModel::DataConnectionFactory* connectionFact;
AbsfactModel::DataCommandFactory* commandFact;
AbsfactModel::DataReaderFactory* readerFact;
void ReadrData()
{
AbsfactModel::DataConnection* data1 = connectionFact->createDataConnection();
AbsfactModel::DataCommand* data2 = commandFact->createDataCommand();
AbsfactModel::DataReader* data3 =readerFact->createDataDataReader();
delete data1;
data1 = nullptr;
delete data2;
data2 = nullptr;
delete data3;
data3 = nullptr;
}
};
int main()
{
AbsfactModel::DataConnectionFactory* cnFact = new AbsfactModel::MySqlConnectionFactory();
AbsfactModel::DataCommandFactory* cdFact = new AbsfactModel::MySqlCommandFactory();
AbsfactModel::DataReaderFactory* rdFact = new AbsfactModel::MySqlDataReaderFactory();
UseData obj(cnFact,cdFact,rdFact);
obj.ReadrData();
return 0;
}
执行结果如下:
备注:也可将上面数据库操作的3个功能接口集中一个抽象接口类中来应对这种变化。
#include
namespace AbsfactModel{
//数据库访问相关操作
class DataConnection
{
public:
DataConnection(){};
virtual ~DataConnection(){};
};
class DataCommand
{
public:
DataCommand(){};
virtual ~DataCommand(){};
};
class DataReader
{
public:
DataReader(){};
virtual ~DataReader(){};
};
class DataFactory
{
public:
virtual DataConnection* createDataConnection()=0;
virtual DataCommand* createDataCommand()=0;
virtual DataReader* createDataDataReader()=0;
};
//MySQL
class MySqlConnection:public DataConnection
{
public:
MySqlConnection()
{
std::cout << "MySqlConnection " << std::endl;
}
~MySqlConnection()
{
std::cout << "~MySqlConnection " << std::endl;
}
};
class MySqlCommand:public DataCommand
{
public:
MySqlCommand()
{
std::cout << "MySqlCommand " << std::endl;
}
~MySqlCommand()
{
std::cout << "~MySqlCommand " << std::endl;
}
};
class MySqlDataReader:public DataReader
{
public:
MySqlDataReader()
{
std::cout << "MySqlDataReader " << std::endl;
}
~MySqlDataReader()
{
std::cout << "~MySqlDataReader " << std::endl;
}
};
class MySqlFactory:public DataFactory
{
public:
virtual DataConnection* createDataConnection()override
{
return new MySqlConnection();
};
virtual DataCommand* createDataCommand()override
{
return new MySqlCommand();
};
virtual DataReader* createDataDataReader()override
{
return new MySqlDataReader();
};
};
//MongoDb
class MongoDbConnection:public DataConnection
{
public:
MongoDbConnection()
{
std::cout << "MongoDbConnection " << std::endl;
}
~MongoDbConnection()
{
std::cout << "~MongoDbConnection " << std::endl;
}
};
class MongoDbCommand:public DataCommand
{
public:
MongoDbCommand()
{
std::cout << "MongoDbCommand " << std::endl;
}
~MongoDbCommand()
{
std::cout << "~MongoDbCommand " << std::endl;
}
};
class MongoDbDataReader:public DataReader
{
public:
MongoDbDataReader()
{
std::cout << "MongoDbDataReader " << std::endl;
}
~MongoDbDataReader()
{
std::cout << "~MongoDbDataReader " << std::endl;
}
};
class MongoDbFactory:public DataFactory
{
public:
virtual DataConnection* createDataConnection()override
{
return new MongoDbConnection();
};
virtual DataCommand* createDataCommand()override
{
return new MongoDbCommand();
};
virtual DataReader* createDataDataReader()override
{
return new MongoDbDataReader();
};
};
}
class UseData
{
public:
UseData(AbsfactModel::DataFactory* cnFt):
pFact(cnFt)
{};
AbsfactModel::DataFactory* pFact;
void ReadrData()
{
AbsfactModel::DataConnection* data1 = pFact->createDataConnection();
AbsfactModel::DataCommand* data2 = pFact->createDataCommand(); //关联性
AbsfactModel::DataReader* data3 = pFact->createDataDataReader();
delete data1;
data1 = nullptr;
delete data2;
data2 = nullptr;
delete data3;
data3 = nullptr;
}
};
int main()
{
AbsfactModel::DataFactory* cFact = new AbsfactModel::MySqlFactory();
UseData obj(cFact);
obj.ReadrData();
return 0;
}
运行结果如下:
抽象工厂方法模式是将有相互依赖的创建工厂集中到一个工厂类中创建,即工厂方法集的类接口。
工厂方法模式也可参考:简单工厂模式与工厂方法模式的C++代码实现_简单工厂模式代码实现_夜雨听萧瑟的博客-CSDN博客