我想使用抽象工厂模式。但是在Factory的构造函数中有一个错误,它说
`[Linker error] undefined reference to `DbDatabaseFactory::CreateConnection()'`
[Linker error] undefined reference to `DbDatabaseFactory::CreateCommand()'
`[Linker error] undefined reference to `DbDatabaseFactory::CreateConnection()'`
[Linker error] undefined reference to `DbDatabaseFactory::CreateCommand()'
这是我的代码:
class IConnection{
public:
bool Connect();
bool Disonnect();
bool connectionState;
};
class ICommand{
public:
void Execute(string);
};
class SqlDbConnection: public IConnection{
public:
bool connectionState;
bool Connect(){
connectionState = true;
cout << "MS Sql bağlantısı açılacak";
return true;
};
bool Disonnect(){
cout << "MS Sql bağlantısı kapatılacak";
return true;
};
};
class MySqlDbConnection:public IConnection{
public:
bool connectionState;
bool Connect(){
connectionState = true;
cout << "MYSql bağlantısı açılacak";
return true;
};
bool Disonnect(){
cout << "MYSql bağlantısı kapatılacak";
return true;
};
};
class SqlDbCommand: public ICommand{
public:
void Execute(string command){
cout << "Sql Command çalıştırılıyor";
};
};
class MySqlDbCommand: public ICommand{
public:
void Execute(string command){
cout << "MySql Command çalıştırılıyor";
};
};
class DbDatabaseFactory{
public:
IConnection CreateConnection();
ICommand CreateCommand();
};
class MsSqlDbFactory: public DbDatabaseFactory{
public:
IConnection CreateConnection(){
SqlDbConnection sqlConnection;
return sqlConnection ;
}
ICommand CreateCommand(){
SqlDbCommand sqlCommand;
return sqlCommand ;
}
};
class MySqlDbFactory: public DbDatabaseFactory{
public:
IConnection CreateConnection(){
MySqlDbConnection mySqlConnection;
return mySqlConnection ;
}
ICommand CreateCommand(){
MySqlDbCommand mySqlCommand;
return mySqlCommand ;
}
};
class Factory{
DbDatabaseFactory _databaseFactory;
IConnection _connection;
ICommand _command;
public:
Factory(DbDatabaseFactory);
void Start(){
_connection.Connect();
if(_connection.connectionState == true){
_command.Execute("SELECT ...");
}
};
};
Factory::Factory(DbDatabaseFactory dbFactory)
{
_databaseFactory = dbFactory;
_connection = dbFactory.CreateConnection();
_command = dbFactory.CreateCommand();
}
你有什么建议吗?
答案 0 :(得分:1)
DbDatabaseFactory的声明应该是,
class DbDatabaseFactory{
public:
virtual IConnection* CreateConnection() = 0;
virtual ICommand* CreateCommand() = 0;
};
同样在Factory类中,您应该保留对DbDatabaseFactory,IConnection和ICommand的指针或引用,以正确使用runtime polymorphism
。
请查看对Factory类的以下更改
class Factory{
DbDatabaseFactory* _databaseFactory;
IConnection* _connection;
ICommand* _command;
public:
Factory(DbDatabaseFactory*);
void Start(){
_connection->Connect();
if(_connection->connectionState == true){
_command->Execute("SELECT ...");
}
};
};
Factory::Factory(DbDatabaseFactory* dbFactory)
{
_databaseFactory = dbFactory;
_connection = dbFactory->CreateConnection();
_command = dbFactory->CreateCommand();
}
这些变化可以解决您的问题。
同时修改MsSqlDbFactory
,如下所示
class MsSqlDbFactory: public DbDatabaseFactory{
public:
IConnection* CreateConnection(){
return new SqlDbConnection();
}
ICommand* CreateCommand(){
return new SqlDbCommand();
}
};