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

Discussion in 'Java' started by Cheok Yan Cheng, Jun 25, 2004.

  1. 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
    Cheok Yan Cheng, Jun 25, 2004
    #1
    1. Advertising

  2. Cheok Yan Cheng

    Oscar kind Guest

    Cheok Yan Cheng <> wrote:
    [...]
    > 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).


    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
    Oscar kind, Jun 25, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Howard
    Replies:
    2
    Views:
    366
    Michiel Salters
    Jul 4, 2003
  2. Rolf Magnus
    Replies:
    1
    Views:
    319
    ding feng
    Jul 4, 2003
  3. snnn
    Replies:
    11
    Views:
    3,374
  4. mazdotnet
    Replies:
    2
    Views:
    389
    Alexey Smirnov
    Oct 2, 2009
  5. Roy Smith
    Replies:
    1
    Views:
    545
    Terry Reedy
    Jun 27, 2012
Loading...

Share This Page