views:

218

answers:

3

I am developing a unit converter for my semester project. I need some help on the structure of the classes/interfaces/abstract classes. I want some interface or abstract class that can be inherited by classes (DegreesConverter, MassConverter, LenghtConverter). Like maybe interface IUnitConvert<Type>. I will be dealing with units of angles (degrees, radians, gradians), force (newtons, pascals) and data (byte, kilobyte, megabyte, gigabyte, terabyte) for starters. Any suggestions?

A: 

There are a lot of different ways you could approach this. Here is one way using delegates.

public class Converter
{
    public static double Convert(double original, Func<double, double> conversion)
    {
        return conversion(original);
    }
}

public class SizeConverter
{
    public static double MegabytesToBytes(double megabytes)
    {
        return megabytes * 1048576;
    }

    public static double KilobytesToBytes(double kilobytes)
    {
        return kilobytes * 1024;
    }
}

You'd use this like this:

        double result1 = Converter.Convert(2, SizeConverter.MegabytesToBytes);
        double result2 = Converter.Convert(2, SizeConverter.KilobytesToBytes);

If you need other types other than doubles, you'll need to overload the Convert method.

mdm20
Only problem with this approach is you get a combinatorial explosion in the SizeConverter enum as you add more units - BytesToKB? MBtoKB? What happens once I add in Gigabytes?
Paolo
Using b/kb/mb/gb/tb there will be 20 methods for all possible conversions, right? I don't think thats unreasonable, considering each conversion is essentially 1 line of code.
mdm20
You are right I don't think this would be something you would have to worry about in the scope of a class project.
macneil
A: 

Think about how you would want to use this, and let that guide you. For example, what kinds of units of measure do you want to represent? What are your base units going to be? How should conversions between units of measure be handled?

Right off I think we can see that you're going to need some way to represent a unit of measurement, e.g. feet, meters, liters, furlongs, degrees, kilograms, pounds, curies, ohms, etc. That appears to be a class, or a series of classes - perhaps Unit as the base, with Feet, Meters, Liters, Furlongs as subclasses. Then I think you'll need some way to associate a unit with a value. This unit/value will have to provide some way to convert between units of measure of the same type (length/distance, volume, temperature, mass/weight) and be bright enough to throw a reasonable exception if the calling code tries something fishy (such as converting 27 degrees Celsius to miles/hour). Some convenient way to create unit/value instances would be handy. The unit/value thing shouldn't be tied to a particular kind or class of unit, but should happily be able to handle any unit you care to throw at it.

Codewise I'd think something like this would be great:

UnitValue a, b, c;

a = new UnitValue(3 * 5280, Feet);
b = new UnitValue(180, Seconds);
c = (a / b).As(Miles / Hours);
cout << c;

should hopefully print something like

60 miles/hour

So you can see that dividing one UnitValue by another UnitValue should produce a new UnitValue with a compound unit - in this case, the a / b should produce a UnitValue with units of Feet per Seconds (feet / seconds), which the conversion routine As then converts to something else, in this case miles per hour.

I hope this helps spark some thinking on your part.

Share and enjoy.

Bob Jarvis
+1  A: 

The first which comes to mind is to have something like System.TimeSpan where the same class represents time and you access it in different units via properties.

You would store the value internally in some constant unit (presumably SI) and convert it in the property getter / setter:

Distance d = new Distance;
d.Meters = 5000;
var km = d.Kilometers; // km = 5;

Or you can create classes for each unit:

public sealed class DistanceUnit
{
    public DistanceUnit(string name, string symbol, double scale)
    {
        Name = name;
        Symbol = symbol;
        Scale = scale;
    }

    public string Name { get; private set; }
    public string Symbol { get; private set; }
    public double Scale { get; private set; }
}


public abstract class Distance
{
    protected Distance(double value)
    {
        this.Value = value;
    }

    protected Distance()
    {
    }

    public double Value { get; set; }

    public abstract DistanceUnit Unit { get; }

    public override string ToString()
    {
        return this.Value + " " + Unit.Symbol;
    }


    public static void Convert<TIn, TOut>(TIn original, out TOut result)
        where TIn : Distance, new()
        where TOut : Distance, new()
    {

        result = new TOut();

        var scale = result.Unit.Scale / original.Unit.Scale;

        result.Value = original.Value * scale;
    }

}

public sealed class Meter : Distance
{

    private static readonly DistanceUnit s_Unit = new DistanceUnit("Meter", "m", 1);

    public Meter(double value) : base(value)
    {
    }

    public Meter()
    {
    }

    public override DistanceUnit Unit
    {
        get { return s_Unit; }

    }
}

public sealed class Kilometer : Distance
{
    private static readonly DistanceUnit s_Unit = new DistanceUnit("Kilometer", "km", .001);

    public Kilometer()
    {
    }

    public Kilometer(double value)
        : base(value)
    {
    }

    public override DistanceUnit Unit
    {
        get { return s_Unit; }
    }
}

which is used like

Meter distanceHome = new Meter(10000);
Kilometer distanceInKMs;

Distance.Convert(distanceHome, out distanceInKMs);   //  distanceInKMs.Value = 10
Courtney de Lautour
Would I use the same Distance.Convert method?
Mohit Deshpande
Yes, it would be the same for any two implementations of Distance (Meter, Feet Mile, etc), which you pass to it.
Courtney de Lautour