I'm extending a class with 10 different constructors. The new subclass, SpecialImage
, is used like this:
SpecialImage specialImage = new SpecialImage(..);
// Leverage the Rotate() method of superclass Image, which
// returns a new rotated image.
// Notice the type is still SpecialImage.
SpecialImage rotated = specialImage.Rotate(...);
SpecialImage
implements 5 of the constructors, each of which takes the same additional parameters, Thing stuff
and int range
:
public class SpecialImage<TDepth> : Image<Gray, TDepth>
where TDepth : new()
{
Thing _stuff;
int _range;
public SpecialImage(Thing stuff, int range) : base()
{
InitParams(stuff, range)
}
public SpecialImage(Thing stuff, int range, Size size) : base(size)
{
InitParams(stuff, range)
}
public SpecialImage(Thing stuff, int range, int width, int height) : base(width, height)
{
InitParams(stuff, range)
}
public SpecialImage(Thing stuff, int range, string filename) : base(filename)
{
InitParams(stuff, range)
}
public SpecialImage(Thing stuff, int range, TDepth[,,] data) : base(data)
{
InitParams(stuff, range)
}
private void InitParams(Thing stuff, int range)
{
_stuff = stuff;
_range = range;
}
}
This is a tad repetitive, which is only really a problem in the face of maintenance. Every time the construction requirements of SpecialImage
change, I have 5 constructors to change.
I sure feel like I should be repeating myself less, but I don't know how to remove this type of redundancy. Is it possible to make this less redundant?