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?
Image
, has a massive number of methods. I'll need many of them, which means I'll need to wrap each of those methods. – kdbanman Jul 10 '15 at 23:02Image rotated = specialImage.Image.Rotate(...);
. I admit that if you need to wrap the output image (rotated
) in aSpecialImage
again then you might need to provide wrapper methods. I think you need to carefully reconsider why you needThing
andRange
, and explore alternative ways of propagating those data. – rwong Jul 11 '15 at 11:07Image rotated = specialImage.Image.Rotate(...);
should beSpecialImage rotated = ...
. I'll edit my question to emphasize that difference's importance. – kdbanman Jul 11 '15 at 17:44