Design Problem: Should Return Type Of A Method As Specific As Possible.

C

Cheok Yan Cheng

one of the rule of thumbs in designing a method is that to make the
return
type of the method as specific as possible (means that we always tend
to
return the lowest level of the inheritance tree)

take the example of one of my method.

the below method will take PlanarImage as my parameter (that is my
wish, i
dun want to make the parameter as RenderedOp type) - RenderedOp is the
subclass of PlanarImage.


private PlanarImage color2Gray(PlanarImage pi) throws Exception
{
assert (pi!=null) : "pi ="+pi;

int numBands = pi.getSampleModel().getNumBands();
boolean hasAlpha = pi.getColorModel().hasAlpha();
double[][] matrix = null;

if(numBands == 1)
{
return pi;
}
else if(numBands == 3)
{
matrix = new double[1][4];
matrix[0][0] = .144D;
matrix[0][1] = .587D;
matrix[0][2] = .299D;
matrix[0][3] = 0;
}
else if(numBands == 4 && hasAlpha)
{
matrix = new double[1][5];
matrix[0][0] = .144D;
matrix[0][1] = .587D;
matrix[0][2] = .299D;
matrix[0][3] = 0;
matrix[0][4] = 0;
}
else
{
throw new Exception("Invalid number of color
bands
= " + numBands + " with alpha component = " + hasAlpha);
}

ParameterBlock pb = new ParameterBlock();
pb.addSource(pi);
pb.add(matrix);

return JAI.create("bandcombine", pb, null);
}

for the last line of statement, should i use

return JAI.create("bandcombine", pb, null); // this one return
RenderedOp

or

return JAI.create("bandcombine", pb, null).getRendering(); // this one
return PlanarImage

for good design purpose?

thank you.

regards
yancheng
 
O

Oscar kind

Cheok Yan Cheng said:
private PlanarImage color2Gray(PlanarImage pi) throws Exception
{ [...]
}

for the last line of [the method], should i use

return JAI.create("bandcombine", pb, null); // this one return
RenderedOp

or

return JAI.create("bandcombine", pb, null).getRendering(); // this one
return PlanarImage

for good design purpose?

Assuming that both options return a correct result, I'd go with the first
option: a RenderedOp is a PlanarImage, so there is no need to complicate
it with further method calls.

Also note that you already fullfilled the contract as specified by the
method declaration and javadoc (the javadoc is absent in this case).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top