dimanche 4 septembre 2022

What is the recommended pattern for 2 classes that are identical apart from constant names?

I have an interface and 2 classes implementing the interface. The only difference between the classes is the constant name, they are being used for the same thing but from different locations. I just want to count how many times the method is called from each different location. Is there a better way of doing this without the repetition or passing in the metric name as a string?

public interface OldIdResolver {

    Optional<String> getNewIdFromOldId();

}

public class CustomFieldIdResolver implements OldIdResolver {

    Optional<String> getIdFromLegacyId(String oldId) {

        Optional<Id> newIdOptional = idService.getNewIdFromOldId(oldId);
        if (newIdOptional.isPresent()) {
            statsDClient.incrementCounter("customField.oldIdUsed");
        }
        return newIdOptional;
    }
}

public class SearcherIdResolver implements OldIdResolver {

    Optional<String> getIdFromLegacyId(String oldId) {

        Optional<Id> newIdOptional = idService.getNewIdFromOldId(oldId);
        if (newIdOptional.isPresent()) {
            statsDClient.incrementCounter("searcher.oldIdUsed");
        }
        return newIdOptional;
    }
}

Aucun commentaire:

Enregistrer un commentaire