lundi 31 janvier 2022

Is there a way I can refactor the code to remove repeated code

I have two classes, both of which implement an interface, but use a different client in their implementations like below:

public interface DDLExecutor {
  public void createDatabase(String databaseName);
}

public class HMSDDLExecutor implements DDLExecutor {
  private final IMetaStoreClient client;

  public HMSDDLExecutor(String tableName) {
    this.client = Hive.get().getMSC();
  }
 
  @Override
  public void createDatabase(String databaseName) {
    try {
      Database database = new Database(databaseName, "automatically created", null, null);
      client.createDatabase(database);
    } catch (Exception e) {
      LOG.error("Failed to create database " + databaseName, e);
      throw new HoodieHiveSyncException("Failed to create database " + databaseName, e);
    }
  }
}

public class ThriftDDLExecutor implements DDLExecutor {
  private final HMSClient client;

  public HMSDDLExecutor(String cfg) {
    this.client = new HMSClient(new URI(cfg.thriftUrl));
  }

  @Override
  public void createDatabase(String databaseName) {
    try {
      Database database = new Database(databaseName, "automatically created", null, null);
      client.createDatabase(database);
    } catch (Exception e) {
      LOG.error("Failed to create database " + databaseName, e);
      throw new HoodieHiveSyncException("Failed to create database " + databaseName, e);
    }
  }
}


How can I refactor it so that DRY principle is followed. Should I try creating a base class?

Aucun commentaire:

Enregistrer un commentaire