2

I have the following classes, which have quite similar method implementations. Only the classes' method inputs and outputs seem to be of different types. When I put it like this, it sounds like a case for inheritance, however, the fact that the inputs and outputs are different and are related to two lambdas, make me wonder if they should remain without any relationship, as one lambda cannot be thought of in place of another (To be a case for inheritance).

My first class looks like the following.

public class JobPersistenceManager {
    private String jobIndexName;
    private JobLambda JobLambda;
    private MyDataPersistence myDataPersistence;
    private DataProcessorUtils dataProcessorUtils;
    private static final String JOB_ID = "jobId";
    private static final String JOB_NAME = "jobName";
@Inject
public JobPersistenceManager(@Named("jobIndex") String jobIndexName,
                             JobLambda JobLambda,
                             MyDataPersistence myDataPersistence) {
    this.jobIndexName = jobIndexName;
    this.JobLambda = JobLambda;
    this.myDataPersistence = myDataPersistence;
    createIndexIfNotExists(this.jobIndexName);
}

public SearchDocumentResult searchJob(MyJobInput myJobInput) throws IOException {
    return myDataPersistence
            .searchDocument(this.jobIndexName,
                    dataProcessorUtils.transformObjectDataPayloadToMap(myJobInput));
}

public MyJobOutput invokeCreateJobLambdaAndIndexData(final MyJobInput myJobInput)
        throws IOException {
    String personRequestPayload = dataProcessorUtils.transformObjectDataInputJson(myJobInput);
    Map<String, String> createdJobOutput = this.JobLambda.invokeLambda(personRequestPayload);
    this.indexCreatedJob(myJobInput, createdPersonOutput);
    return MyJobOutput.builder().withJobID(createdJobOutput.get(JOB_ID))
            .withJobName(createdJobOutput.get(JOB_NAME)).build();
}

public int indexCreatedJob(final MyJobInput myJobInput,
                           final Map<String, String> createdJobOutput) throws IOException {
    myJobInput = modifyJobInput(myJobInput);
    String documentToIndex = dataProcessorUtils.transformObjectDataInputJson(myJobInput);
    return myDataPersistence.indexDocument(this.jobIndexName, documentToIndex);
}

private void createIndexIfNotExists(final String indexName) {
    if (!myDataPersistence.doesIndexExist(indexName)) {
        myDataPersistence.createIndex(CreateIndexInput.builder().indexName(indexName).build());
    }
}

}

My second class looks like the following.

public class EmployeePersistenceManager {
    private EmployeeLambda employeeLambda;
    private MyTestDataPersistence myTestDataPersistence;
    private DataProcessorUtils dataProcessorUtils;
    private String employeeIndexName;
    private static final String PERSON_ID_KEY = "personId";
    private static final String PERSON_NAME_KEY = "personName";
@Inject
public EmployeePersistenceManager(@Named("employeeIndex") String employeeIndexName,
                                EmployeeLambda employeeLambda,
                                MyTestDataPersistence myTestDataPersistence,
                                DataProcessorUtils dataProcessorUtils) {
    this.employeeIndexName = employeeIndexName;
    this.employeeLambda = employeeLambda;
    this.myTestDataPersistence = myTestDataPersistence;
    this.dataProcessorUtils = dataProcessorUtils;
    createIndexIfNotExists(employeeIndexName);
}

public SearchDocumentResult searchPerson(EmployeeInput employeeInput) throws IOException {
    return myTestDataPersistence
            .searchDocument(employeeIndexName,
                    dataProcessorUtils.transformObjectDataPayloadToMap(employeeInput));
}

public EmployeeOutput invokeCreatePersonLambdaAndIndexData(final EmployeeInput employeeInput)
        throws IOException {
    String personRequestPayload = dataProcessorUtils.transformObjectDataInputJson(employeeInput);
    Map<String, String> createdPersonOutput = this.employeeLambda.invokeLambda(personRequestPayload);
    this.indexCreatedEmployee(employeeInput, createdPersonOutput);
    return EmployeeOutput.builder().withPersonId(createdPersonOutput.get(PERSON_ID_KEY))
            .withPersonName(createdPersonOutput.get(PERSON_NAME_KEY)).build();
}

public int indexCreatedEmployee(final EmployeeInput employeeInput,
                              final Map<String, String> createdPersonOutput) throws IOException {
    employeeInput = modifyEmployeeInput(employeeInput);
    String documentToIndex = dataProcessorUtils.transformObjectDataInputJson(employeeInput);
    return myTestDataPersistence.indexDocument(this.employeeIndexName, documentToIndex);
}

public Map.Entry<String, Map<String, String>> invokeLambda(final String payload) {
    return new AbstractMap.SimpleEntry<>(payload, this.employeeLambda.invokeLambda(payload));
}

private void createIndexIfNotExists(final String indexName) {
    if (!myTestDataPersistence.doesIndexExist(indexName)) {
        myTestDataPersistence.createIndex(CreateIndexInput.builder().indexName(indexName).build());
    }
}

}

As you can see, the methods perform almost the same actions. Only the indexCreatedEmployee and indexCreatedJob methods from the classes have an extra step of processing the input.

Should I keep these classes as they are now without any relationships between them, or should I create an abstract persistence manager class and perform the following.

  1. Move createIndexIfNotExists to the abstract class
  2. Create abstract methods search(), invokeLambda() and indexCreatedData() methods and implement them in each child class. The data types MyJobInput and MyEmployeeInput are POJO classes that don't have any relationship. So I guess these methods I mentioned would then take "Object" parameters?

EmployeeLambda and JobLambda are again classes with no relationship between them. Another concern I had towards creating some sort of inheritance was that, Employee Lambda and JobLambda cannot be used inter-changeably. So was wondering if they should inherit the same parent class just because they're both lambda classes.

OR is there another way to go about this? Any advice would be much appreciated. Thank you very much in advance.

AnOldSoul
  • 173

1 Answers1

3

As you say, both classes do the same, abstractly speaking. Perhaps it would be worth trying this:

  • Forget about inheritance here, it would add unnecesary complexity in this specific case. The behavior is almost the same, and even if it were behavior that changed, it's better to do it by injecting it (composition)
  • Extract interfaces from both classes (IDE will help)
  • Compare both interfaces
  • Merge both interfaces by generalizing names like, for example changing EmployeePersistenceManager to PersistenceManager, so you end up with only one interface implemented by the two classes
  • Generalize the parameters via Generics
  • Extract some interfaces from the parameter objects as well or use one common interface they already implement which I'm almost sure they already do.
  • With some minor changes (like renaming JOB_ID to just ID ) you can keep just one of the two classes and deprecate the other.

Furthermore, I bet you will end up using the resulting generic class for "managing the persistence" of things other than Employees and Jobs.

Tulains Córdova
  • 39,570
  • 13
  • 100
  • 156