I am looking for feedback on a design I did for a custom adapter class in java. I am trying to create an elegant solution for an abstract adapter class that can be extended to create unique adapters that can connect to different data stores. Here are some of the requirements
- Each adapter must implement an initialize function that establishes the connection
- Each adapter must implement an execute function
- The initialize function must be called when a new instance of the adapter class is created
- Each adapter constructor must take in a Configuration object that contains all the connection information for that specific adapter.
- Configurations are unique to the adapter, but I want a way to enforce that the configuration gets read in and set to a config variable either at the abstract class level or the base class level.
I also chose to implement a factory pattern when creating a new adapter here is my code below:
BaseAdapter.java
public abstract class BaseAdapter {
private Configuration config;
/*
Default constructor
*/
protected BaseAdapter(Configuration config) {
this.config = config;
}
/*
Abstract method that will initialize adapter with
Configuration properties
*/
public abstract void initialize(Configuration config);
/*
Abstract method that will execute query
*/
public abstract void execute(String query);
ElasticSearchAdapter.java
public class ElasticSearchAdapter extends BaseAdapter {
public ElasticSearchAdapter(Configuration config) {
super(config);
initialize(config);
}
@Override
public void initialize(Configuration config) {
//initialization implementation
}
@Override
public void execute(String query) {
//execute query
}
}
BaseAdapterFactory.java
public class BaseAdapterFactory {
private static final String ES = "elasticsearch";
public static BaseAdapter getBaseAdapter(String type, Configuration config) {
if(ES.equals(type)) {
return new ElasticSearchAdapter(config);
}
else {
return null;
}
}
}
I was curious if there is a better way to design this based on the requirements stated above.
Aucun commentaire:
Enregistrer un commentaire