Call unique methods without instanceof

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 2201 Views - Last Post: 28 November 2012 - 08:05 PM Rate Topic: -----

#31 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5805
  • View blog
  • Posts: 12,643
  • Joined: 16-October 07

Re: Call unique methods without instanceof

Posted 28 November 2012 - 01:59 PM

Think about how it will all work when you're using the final product and work backward.

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. :P
Was This Post Helpful? 0
  • +
  • -

#32 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Call unique methods without instanceof

Posted 28 November 2012 - 08:05 PM

30 replies for such a basic subject
Don't you think it is enough ?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3