File - DatabaseOperations.h
This file contain classes representing database operations for three different types of databases (relational, document based and graph based) like establishing connections, executing statements, and committing and closing the connections.
There are four abstract classes with one pure virtual functions each.
The class
DatabaseConnection
contains pure virtual functionconnect()
- this class is inherited by three concrete classes representing three different types of databases each one of those implement theconnect()
function.The class
ExecuteStatements
contains one pure virtual functionExecute()
- this class is again inherited by three concrete classes representing three different types of databases and each one of those implement theExecute()
function.The class
CommitDisconnect
contains one pure virtual functioncommit()
- this class is yet again inherited by three concrete classes representing three different types of databases and each one of those implement thecommit()
function.The class
DatabaseOperation
which aggregates all the operations and contains three pure virtual functions,connect
,execute
andcommit
. All these three virtual functions are implemented in three respective classes.
#include <iostream>
#include <string>
/****************************************************************
* Connection
*****************************************************************/
class DatabaseConnection
{
public:
virtual void connect() = 0;
};
class RelationalDatabaseConnection : public DatabaseConnection
{
public:
void connect() override
{
std::cout << "Connection to RDBMS\n";
}
};
class DocumentDatabaseConnection : public DatabaseConnection
{
public:
void connect() override
{
std::cout << "Connection to Document DBMS\n";
}
};
class GraphDatabaseConnection : public DatabaseConnection
{
public:
void connect() override
{
std::cout << "Connection to Graph DBMS\n";
}
};
/****************************************************************
* Execute Statements
*****************************************************************/
class ExecuteStatements
{
public:
virtual void Execute() = 0;
};
class RelationalDatabaseExecute : public ExecuteStatements
{
public:
void Execute() override
{
std::cout << "Executing ANSI SQL\n";
}
};
class DocumentDatabaseExecute : public ExecuteStatements
{
public:
void Execute() override
{
std::cout << "Executing JSON/Parquet etc.\n";
}
};
class GraphDatabaseExecute : public ExecuteStatements
{
public:
void Execute() override
{
std::cout << "Creating Nodes and Edges\n";
}
};
/****************************************************************
* Commit & Disconnection
*****************************************************************/
class CommitDisconnect
{
public:
virtual void commit() = 0;
};
class RelationalCommitDisconnect : public CommitDisconnect
{
public:
void commit() override
{
std::cout << "Committing and closing RDBMS connection\n";
}
};
class DocumentCommitDisconnect : public CommitDisconnect
{
public:
void commit() override
{
std::cout << "Committing and closing Document DBMS connection\n";
}
};
class GraphCommitDisconnect : public CommitDisconnect
{
public:
void commit() override
{
std::cout << "Committing and closing Graph DBMS connection\n";
}
};
class DatabaseOperation
{
public:
virtual DatabaseConnection* connect() = 0;
virtual ExecuteStatements* execute() = 0;
virtual CommitDisconnect* commit() = 0;
};
class Relational : public DatabaseOperation
{
public:
DatabaseConnection* connect() override
{
return new RelationalDatabaseConnection();
}
ExecuteStatements* execute() override
{
return new RelationalDatabaseExecute();
}
CommitDisconnect* commit() override
{
return new RelationalCommitDisconnect();
}
};
class Document : public DatabaseOperation
{
public:
DatabaseConnection* connect() override
{
return new DocumentDatabaseConnection();
}
ExecuteStatements* execute() override
{
return new DocumentDatabaseExecute();
}
CommitDisconnect* commit() override
{
return new DocumentCommitDisconnect();
}
};
class Graph : public DatabaseOperation
{
public:
DatabaseConnection* connect() override
{
return new GraphDatabaseConnection();
}
ExecuteStatements* execute() override
{
return new GraphDatabaseExecute();
}
CommitDisconnect* commit() override
{
return new GraphCommitDisconnect();
}
};
file - main.cxx
#include <iostream>
#include "DatabaseOperations.h"
int main()
{
int value = -1;
DatabaseOperation* operation = nullptr;
std::cout << "Relational: 1, Document: 2 and Graph: 3\n" << "Enter: ";
std::cin >> value;
switch (value)
{
case 1:
operation = new Relational();
break;
case 2:
operation = new Document();
break;
case 3:
operation = new Graph();
default:
break;
}
if (operation != nullptr)
{
operation->connect()->connect();
operation->execute()->Execute();
operation->commit()->commit();
}
return 0;
}
Does the above code implements the Abstract Factory design pattern correctly?