1

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.

medium
  • 4,136
  • 16
  • 55
  • 66
  • 1
    You should be careful with [overridable method calls in constructors](https://stackoverflow.com/q/3404301/3182664). You're already using a factory. Couldn't the factory call the `initialize` method? (Probably guarded by a flag in the implementation, so that calling it again will not have any effect). Just a first thought, though. – Marco13 Nov 27 '18 at 19:20
  • @Marco13 I knew something was off with the initialize. I like the idea of putting it in the factory. Thanks for the tip – medium Nov 27 '18 at 20:38

1 Answers1

1

The Factory Pattern is the way to go there. You might also want to explore the option to avoid modifying the factory each time you add a new adapter. Remember the Open-Close principle: open for extension closed for modifications.

You can use implements a factory with class registration.

adiian
  • 1,382
  • 2
  • 15
  • 32