As I am maintaining and extend a software system in Java, I saw a colleague (who left due to retirement) implementing a table with a generic approach. This approach is unluckily bound to tables with a key-value appearance.
Description 1 | Value 1
Description 2 | Value 2
Description 3 | Value 3
There are multiple tables, where this approach fits the requirement in an optimal way. Values and description are stored in enums.
Now, I have to face the situation, that there should be more than one column with values. E.g.:
Description 1 | Value1 1 | Value2 1 |…
Description 2 | Value1 2 | Value2 1 |…
Description 3 | Value1 3 | Value2 1 |…
For the columns applies that they are all similar. The only difference they have is in an SQL, where the value is requested.
E.g. Value1 1 SELECT * FROM ZOO WHERE […] AND LOCATION = ‘CA’
Value2 1 SELECT * FROM ZOO WHERE […] AND LOCATION = ‘UA’
This value is/was constant in the enum and seems to be the breakpoint, breaking this hard-wired construct.
One approach would be to create the same objects in the enum again with passing the location as additional parameter. This way would result in having:
– Value1 1_CA
– Value2 1_UA
Which seems to create a big overhead, since the objects only differ in location, but all other parameters are the same.
Another way I thougt of was refactoring the enum to an abstract class and derive from it, so that I have the base values and only objects/class like following
- CA extends BaseClass
- UA extends BaseClass
However this seems not like a clean approach, more like an “make it any case works” approach. Inheritance would be used in the way it shouldn’t be, so I thought of delegation. But for delegation, the BaseClass must be instantiable (which is incorrect too).
How could this problem be solved without breaking the concept of clean OOP?