HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Refactoring Java class with lots of constants

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
refactoringwithjavalotsclassconstants

Problem

class MyObject
{
    public static enum Type {A, B, C, D;}

    public static final int ID_MAIN = 1;
    public static final int ID_MAIN_UK = 2;
    public static final int ID_MAIN_US = 3;
    public static final int ID_SUB = 4;
    // lots more constants here

    public static final String DESCRIPTION_1 = "Desc Full Name";
    public static final String DESCRIPTION_2 = "Desc2 Full Name";
    // lots more constants here

    private int id;

    public MyObject(final int id)
    {
        this.id = id;
    }

    //simple getter 
    public int getID() { return this.id;}

    // real responsibility of the class is in the following two methods
    public static String getDescription()
    {
         switch(id)
         {
              case MyObject.ID_MAIN:
              case MyObject.ID_MAIN_UK:
                  return MyObject.DESCRIPTION_1;
              case MyObject.ID_SUB:
                  return MyObject_Description_2;
              default:
                   // throw IllegalArgException
         }        
    }

    public static Type getType(int id)
    {
         switch(id)
         {
             case MyObject.ID_MAIN:
             case MyObject.ID_SUB:
                 return Type.A;
             case MyObject.ID_MAIN_UK:
             case MyObject.ID_MAIN_US:
                 return Type.B;
             default:
                 return Type.Undefined;
         }
     }
 }


Basically, there is an ID that maps to both a description and a type. This ID is passed in during construction of the class and it should map to a set of constants already contained in the class. If the id is not part of the list of constants, an error is thrown when trying to get the description that maps to the id and an 'Unknown' type is return if the type is queried. The ID maps a description to a set of constants. The same ID maps to a certain Type (defined as an enum).

This code is pretty ugly because there are tons of constants defined at the top, which makes the sw

Solution

This is a classic case for using an Enum....

public enum MyEnum {
    ID_MAIN("Desc Full Name", Type.A),
    ID_MAIN_UK("Desc Full Name", Type.B),
    ID_SUB("Desc1 Full Name", Type.A),
    .....

    private final String description;
    private final Type type;

    private MyEnum(String desc, Type type) {
        this.type = type;
        this.description = desc;
    }

    public int getID() {
        return ordinal() + 1;
    }

    public String getDescription() {
        return description;
    }

    public Type getType() {
        return type;
    }
}

Code Snippets

public enum MyEnum {
    ID_MAIN("Desc Full Name", Type.A),
    ID_MAIN_UK("Desc Full Name", Type.B),
    ID_SUB("Desc1 Full Name", Type.A),
    .....



    private final String description;
    private final Type type;

    private MyEnum(String desc, Type type) {
        this.type = type;
        this.description = desc;
    }

    public int getID() {
        return ordinal() + 1;
    }

    public String getDescription() {
        return description;
    }

    public Type getType() {
        return type;
    }
}

Context

StackExchange Code Review Q#39929, answer score: 2

Revisions (0)

No revisions yet.