quantity of measurable units design pattern
Posted
by Berryl
on Stack Overflow
See other posts from Stack Overflow
or by Berryl
Published on 2010-04-19T19:15:19Z
Indexed on
2010/04/19
19:23 UTC
Read the original article
Hit count: 338
design-patterns
|c#
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);
© Stack Overflow or respective owner