vendredi 17 juillet 2015

Code duplication in enums inheriting a common interface

I have several enums which comply to a common interface:

    interface TableColumns
    {
      String getColumnName();
      int getColumnIndex();
      ColumnType getColumnType();
      boolean isEditable();
      int getColumnWidth();
    }

A typical implementation is:

enum PointsTable implements TrendTableColumns
{
    POINTS_COLUMN("points_column", false, ColumnType.TEXT, 400, 0);

    private String columnName;
    private boolean editable;
    private ColumnType columnType;
    private int columnWidth;
    private int columnIndex;

    private PointsTable(String columnName, boolean editable, ColumnType columnType,
        int columnWidth,
        int columnIndex)
    {
        this.polarColumnName = columnName;
        this.editable = editable;
        this.columnType = columnType;
        this.columnWidth = columnWidth;
        this.columnIndex = columnIndex;
    }

    public boolean isEditable()
    {
        return editable;
    }

    public int getColumnIndex()
    {
        return columnIndex;
    }

    public String getColumnName()
    {
        return polarColumnName;
    }

    public int getColumnWidth()
    {
        return columnWidth;
    }

    public ColumnType getcolumnType()
    {
        return columnType;
    }
}

I have several implementaions like this (10+). Now, the problem is that I see a lot of code duplication here, as the methods in all of the implementation are identical word by word. I know that in Java this is virtually impossible as enums can't extend an implementation. What I need here is a suggestion or a different strategy where this can be done in a cleaner way. Is there already some existing pattern regarding this?

Aucun commentaire:

Enregistrer un commentaire