I have a class that I use to represent a distance called Dimension
.
Here is its four constructors:
/// <summary>
/// Accepts any valid architectural string value for input.
/// </summary>
public Dimension(string passedArchitecturalString)
{
storeArchitecturalStringAsInternalUnit(passedArchitecturalString);
}
/// <summary>
/// Accepts standard types for input.
/// </summary>
public Dimension(DimensionType passedDimensionType, double passedInput)
{
storeAsInternalUnit(passedDimensionType, passedInput);
}
/// <summary>
/// copy constructor - create a new Dimension with the same _intrinsicValue as the passed Dimension
/// </summary>
public Dimension(Dimension passedDimension)
{
_intrinsicValue = passedDimension._intrinsicValue;
}
/// <summary>
/// Zero Constructor
/// </summary>
public Dimension()
{
_intrinsicValue = 0;
}
This question gives some more background on my Dimension Class.
Because it is a class that I use so often and it usually uses inches. I had to write out the one of the full constructors every time that I wanted to use it.
Dimension inchDimension = new Dimension(DimensionType.Inch, 14.1875);
This became a major pain when creating things like my 3 dimensional Point object:
Dimension xDimension = new Dimension(DimensionType.Millimeter, 0);
Dimension yDimension = new Dimension(DimensionType.Millimeter, 0);
Dimension zDimension = new Dimension(DimensionType.Millimeter, 0);
Point p = new Point(xDimension, yDimension, zDimension);
So I created a class to generate these objects called DimensionGenerator
.
public class DimensionGenerator
{
/// <summary>
/// Default generator sets to inches
/// </summary>
public DimensionGenerator()
{
_internalType = DimensionType.Inch;
}
/// <summary>
/// This constructor allows you to specify a static type and change it later
/// </summary>
/// <param name="wantedType">DimensionType to use for now</param>
public DimensionGenerator(DimensionType wantedType)
{
_internalType = wantedType;
}
DimensionType _internalType;
public DimensionType DimensionOutputType
{
get { return _internalType; }
set { _internalType = value; }
}
public Dimension MakeDimension(double passedValue)
{
return new Dimension(_internalType, passedValue);
}
}
I seem to use it a LOT. its less typing to just create on of these instead of using a Dimension constructor. Especially when writing Unit Tests where numbers may not really matter and all I care about are their equality or etc. Isn't it terribly wasteful to create a new one of these in most methods in my application? Should I make a bunch of static DimensionGenerators
that only output a specific type? What should I consider when having a class like this?