Pergunta

I want to use abstract factory pattern. But in the contructor of Factory there is an error and it says

    `[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()' 

Here is my code:

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();
}

Do you have any suggestion?

Foi útil?

Solução

Declaration of DbDatabaseFactory should have been,

  class DbDatabaseFactory{
  public:
         virtual IConnection* CreateConnection() = 0;
         virtual ICommand* CreateCommand() = 0;
  };

And also in the Factory class, you should keep pointers or references to DbDatabaseFactory,IConnection and ICommand to employ runtime polymorphism properly.

Please find the following changes to Factory class,

 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();
  } 

These changes should solve your problems.

Also modify your MsSqlDbFactory as below

class MsSqlDbFactory: public DbDatabaseFactory{
  public:
         IConnection* CreateConnection(){
          return new SqlDbConnection();
         }

         ICommand* CreateCommand(){
          return new SqlDbCommand(); 
         }
  };
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top