dimanche 20 novembre 2016

OOP - best approach for adding property to class

We develop an HR application written in Java and mapped with Hibernate; one of the feature is the phase of recruiting.

The Candidate class is modeled like this:

public class Candidate  {
    private String id;
    private Integer candidateCode;
    private GregorianCalendar birthDate;
    private String italianFiscalCode; //unique code for italian people 
}

Since we developed for a market only till now the code is very dependent on a particular legislation, look at the fiscalCode class property.

The request is that we generalize this concept to be able to expand to other markets, where for example the unique identifier can be different, can be composed of several strings or be absent at all.

First thing that popped in my mind:

1 - Simply rename the field as countryIdentifier and add other fields if needed for particular countries.

private String countryIdentifier; //general unique code
private Integer greekAddedCode;   

This means refactoring the code where needed (all the placed where the old italianFiscalCode gets used), renaming a DBMS column (and adding others eventually) and modifying all the queries that use that field.

This looks like a poor implementation to me

2 - Subclass Candidate creating ItalianCandidate and GreekCandidate and move the particular field in the subclasses.

The problem is that the Candidate class is already subclassed by HeavyCandidate that has the sole function to optimize the Hibernate mapping since we move all the "heavy" properties (many-to-ones and sets) in the heavy class (this is an approach that we follow with all our beans).

In this situation what is the most correct approach?

Aucun commentaire:

Enregistrer un commentaire