Hello
I am thinking through a nice pattern to be useful across domains of measurable units (ie, Length, Time) and came up with the following use case and initial classes, and of course, questions!
1) Does a Composite pattern help or complicate?
2) Should the Convert method(s) in the ComposityNode be a separate converter class?
All comments appreciated. Cheers,
Berryl
Example Use Case:
var inch = new ConvertableUnit("inch", 1)
var foot = new ConvertableUnit("foot", 12)
var imperialUnits = new CompositeConvertableUnit("imperial units", .024)
imperialUnits.AddChild(inch)
imperialUnits.AddChild(foot)
var meter = new ConvertableUnit("meter", 1)
var millimeter = new ConvertableUnit("millimeter ", .001)
var imperialUnits = new CompositeConvertableUnit("metric units", 1)
imperialUnits.AddChild(meter)
imperialUnits.AddChild(millimeter)
var oneInch = new Quantity(1, inch);
var oneFoot = new Quantity(1, foot);
oneFoot.ToBase() // "12 inches"
var oneMeter = new Quantity(1, meter);
oneInch.ToBase() // .024 meters
Possible Solution
ConvertableUnit : Node
double Rate
string Name
Quantity
ConvertableUnit Unit
double Amount
CompositeConvertableUnit : Node
ISet<ConvertableUnit> _children
ConvertableUnit BaseUnit {get{ return _children.Where(c=>c.Rate == 1).First() } }
Quantity ConvertTo(Quantity from, Quantity to)
Quantity ToBase(Quantity from);