I need to implement some kind of simple database client, which can perform CRUD operations on a database. However, it should be designed in such a way that it is easily possible to replace the backend (e.g. mongoDB) with another one (e.g. postgres) or whatever.
What design patterns would you suggest to implement?
As a very simple approach, I would simply create an interface which defines methods for all the CRUD operations:
interface DBDriver {
void write(String data);
String read();
...
}
Classes implementing this interface would then be injected in a Client
class like:
class DBClient() {
public DBClient(DBDriver dbDriver) {
...
}
//methods like write, read, update ...
}
Aucun commentaire:
Enregistrer un commentaire