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

Java design for dimensions and units

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

Problem

I created this abstract class:

public abstract class MeasurementType {
  protected String name;
  protected String[] units;

  public MeasurementType() {}

  public MeasurementType(String name) {
    this.name = name;
  }

  protected void setName(String name) {
    this.name = name;
  }

  protected String getName() {
    return name;
  }

  protected void setUnits(String[] values) {
    this.units = values;
  }
}


Then I thought to create to other classes that were its subtypes:

public class Acceleration extends MeasurementType {
  private String name;

  public Acceleration () {
    super();
  }

  public Acceleration(String name) {
    super();
  }
}

public class Density extends MeasurementType {
  private String name;

  public Density() {
    super();
  }

  public Density(String name) {
    super();
  }
}


Plenty to choose from but these were the first two types that cam to my mind, the first question might be what the need for the subtypes are since there isn't anything in those classes that differentiates them apart enough (at least in my implementations) to give them the right to exist as their own entities, but for the sake of the example, lets say they had their own arrays containing units for each distinct type, so density would have kilograms, grams etc.. and acceleration would have mph, kmph etc..

Finally, I have this run class which I quickly made up:

```
public class run {

public run() {}

public static MeasurementType testType() {
Acceleration a = new Acceleration("meters per second");
return (MeasurementType) a;
}

public static void main(String[] args) {
MeasurementType mt = testType();
System.out.println(mt.getClass().toString());
System.out.println(mt.getClass().getName());

String type = "acceleration";
String[] units = new String[8];

Object mType;
if(type.equals("acceleration")) {
mType = new Acceleration();
} else if(type.equals("density")) {
mType = new Den

Solution

Using strings in themselves to determine the MeasurementType isn't the biggest problem, but you really don't need to create classes and subclasses and stuff for this.

Instead, use an enum!

public enum MeasurementType {
    DENSITY, ACCELERATION;
}


In this case you can use this code to grab the MeasurementType:

String type = ...;
MeasurementType mType = MeasurementType.valueOf(type.toLowerCase());


Note however that this will throw an exception if the type is not found. So instead, you might want to consider using a Map where you add all known MeasurementTypes (preferably in a static initializer block), or you can loop through MeasurementType.values() and scan for the one with the right name.

Also remember that enums can have methods, implement interfaces, override methods, and contain fields.

As I'm not sure what your ultimate goal is, here's an example of what an enum can do:

public enum MeasurementType {
    DENSITY("test"),
    ACCELERATION("test2") {
        @Override
        public int transform(int value) {
            return 0;
        }
    },
    SOMETHING_ELSE("test3")
    ;

    private final String test;

    private MeasurementType(String test) {
        this.test = test;
    }

    public int transform(int value) {
        return value;
    }
}

Code Snippets

public enum MeasurementType {
    DENSITY, ACCELERATION;
}
String type = ...;
MeasurementType mType = MeasurementType.valueOf(type.toLowerCase());
public enum MeasurementType {
    DENSITY("test"),
    ACCELERATION("test2") {
        @Override
        public int transform(int value) {
            return 0;
        }
    },
    SOMETHING_ELSE("test3")
    ;

    private final String test;

    private MeasurementType(String test) {
        this.test = test;
    }

    public int transform(int value) {
        return value;
    }
}

Context

StackExchange Code Review Q#51068, answer score: 5

Revisions (0)

No revisions yet.