So, you want a unit converter? What are the fundamental characteristics of Unit and how will it work?
I imagine something like:
interface Unit {
Unit convert(Unit from);
boolean canConvert(Unit from);
String getUnitName();
String getUnitType();
double getValue();
void setValue(double value);
double getBaseValue(); // this is the secret to most converters
// String toString(); implicit in class
}
How much of that can you stuff into a base class? Probably most of it:
abstract class UnitBase implements Unit {
protected double value;
protected final double factor;
protected final String unitName;
protected abstract UnitBase getInstance();
public abstract String getUnitType();
protected UnitBase(String unitName, double factor) {
this.unitName = unitName;
this.factor = factor;
this.value = 1;
}
public boolean canConvert(Unit from) { return this.getUnitType().equals(from.getUnitType()); }
public String getUnitName() { return this.unitName; }
public double getValue() { return this.value / factor; }
public void setValue(double value) { this.value = value * factor; }
public double getBaseValue() { return this.value; }
public Unit convert(Unit from) {
if (!canConvert(from)) { return null; }
UnitBase unit = getInstance();
unit.value = from.getBaseValue();
return unit;
}
public String toString() { return "" + getValue() + " " + getUnitName(); }
}
The kids are simple:
class Length extends UnitBase {
public Length(String unitName, double factor) { super(unitName, factor); }
protected UnitBase getInstance() { return new Length(unitName, factor); }
public String getUnitType() { return "Length"; }
}
class Currency extends UnitBase {
public Currency(String unitName, double factor) { super(unitName, factor); }
protected UnitBase getInstance() { return new Length(unitName, factor); }
public String getUnitType() { return "Currency"; }
}
Throw in your other requirments:
interface UnitGroup {
String getGroupName();
List<Unit> getUnits();
}
interface UnitGroupProvider {
List<UnitGroup> provideGroups();
}
Well, I've already gone a little overboard. Might as well test it:
UnitGroupProvider gp = new UnitGroupProvider() {
private List<UnitGroup> list = null;
public List<UnitGroup> provideGroups() {
if (list==null) {
list = new ArrayList<UnitGroup>();
list.add(new UnitGroup() {
public String getGroupName() { return "Length"; }
public List<Unit> getUnits() {
List<Unit> list = new ArrayList<Unit>();
list.add(new Length("Centimeter", 1.0));
list.add(new Length("Inch", 2.54));
list.add(new Length("Foot", 30.48));
list.add(new Length("Yard", 91.44));
list.add(new Length("Meter", 100.0));
return list;
}
});
list.add(new UnitGroup() {
public String getGroupName() { return "Currency"; }
public List<Unit> getUnits() {
List<Unit> list = new ArrayList<Unit>();
// Read from a web service or whatevewr
return list;
}
});
}
return list;
}
};
for(UnitGroup ug : gp.provideGroups()) {
System.out.println(ug.getGroupName());
List<Unit> units = ug.getUnits();
for(Unit u1 : units) {
u1.setValue(1);
for(Unit u2 : units) {
System.out.println(u1 + " = " + u2.convert(u1));
}
}
}
Looks good. Don't know if that helps you at all. If nothing else, there are clearly innumerable ways to look at a problem.

New Topic/Question
Reply





MultiQuote


|