数据库客户端设计支持不同的后端

时间:2022-11-24 12:49:43

What are usable design patterns to use for implementing a data base client capable of supporting various backends? (e.g. mongodb, or postgres,...)

用于实现能够支持各种后端的数据库客户机的可用设计模式有哪些?(如mongodb或postgres…)

As a very simple approach, I would simply create an interface which defines methods for all the CRUD operations:

作为一种非常简单的方法,我只需创建一个接口,为所有CRUD操作定义方法:

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 ...

}

1 个解决方案

#1


2  

Adapter to encapsulate database specific details while exposing a common interface.

适配器,用于在公开公共接口的同时封装数据库特定的细节。

Factory Method to instantiate the correct adapter.

工厂方法来实例化正确的适配器。

The DAO pattern is essentially an adapter.

DAO模式本质上是一个适配器。

#1


2  

Adapter to encapsulate database specific details while exposing a common interface.

适配器,用于在公开公共接口的同时封装数据库特定的细节。

Factory Method to instantiate the correct adapter.

工厂方法来实例化正确的适配器。

The DAO pattern is essentially an adapter.

DAO模式本质上是一个适配器。