抽象工厂
抽象工厂
提供一个接口,让该接口负责创建一系列“相关或者相互依赖的对象”,无需指定它们具体的类。
class DBConnection
{
public:
DBConnection(){ }
virtual ~DBConnection(){ }
virtual bool connectionString() = 0;
}
class DBCommand
{
public:
DBCommand(){ }
virtual ~DBCommand(){ }
virtual void setConnection() = 0;
virtual bool commandText() = 0;
virtual DBDataReader* executeReader() = 0;
}
class DBDataReader
{
public:
DBDataReader(){ }
virtual ~DBDataReader(){ }
virtual bool read() = 0;
}
class DBFactory
{
public:
DBFactory(){ }
virtual ~DBFactory(){ }
virtual DBConnection* createDBConnection() = 0;
virtual DBCommand* createDBCommand() = 0;
virtual DBDataReader* createDBDataReader() = 0;
}
class Employees
{
public:
void getEmployees()
{
DBConnection* connection=factory_->createDBConnection();
connection->connectionString();
DBCommand* command=factory_->createDBCommand();
command->setConnection();//相关联
command->commandText();
DBDataReader* reader=command->executeReader();//相关联
while(reader->read())
{
}
}
private:
DBFactory* factory_;
}
class MysqlDBConnection :
{
public:
virtual bool connectionString() overrive
{
//...
}
}
class DBCommand
{
public:
virtual void setConnection()
{
//...
}
virtual bool commandText()
{
//...
}
virtual DBDataReader* executeReader()
{
//...
}
}
class DBDataReader
{
public:
virtual bool read()
{
//...
}
}
class DBFactory
{
public:
virtual DBConnection* createDBConnection()
{
//...
}
virtual DBCommand* createDBCommand()
{
//...
}
virtual DBDataReader* createDBDataReader()
{
//...
}
}