Re: "Program to an interface" - When to break a design pattern

Discussion in 'Java' started by Roedy Green, May 6, 2011.

  1. Roedy Green

    Roedy Green Guest

    On Thu, 05 May 2011 12:21:55 -0700, Zapanaz
    <http://joecosby.com/code/> wrote, quoted or indirectly
    quoted someone who said :

    >The problem is, where I'm creating sortedMap above, I need the map to
    >retain the insertion order. If what's returned actually is a Map,
    >rather than a LinkedHashMap, then the results the user actually sees
    >are going to be in the wrong order. Making things worse, in this case,
    >nothing would actually break, only the end user would notice anything
    >was actually wrong.


    The idea of an interface is you should be able to substitute some
    other implementation and all should work unmodified. That is not the
    case with the interface you chose. You need to either find or create
    an interface that nails down all the desired behaviour, or use the
    original actual implementation.

    I am not a big fan of this pattern for the following reasons:
    1. it slows down access.
    2. it obfuscates just what the code is using NOW for the
    implementation.
    3. It is more verbose, giving you one more thing you have to eyeball
    carefully and make dance through the genericity hoops. That makes for
    buggier code.

    The main place it would pay off is if you plan to later do some fine
    tuning, and want to leave open the option of easily substituting your
    own tuned collection classes.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    Politicians complain that Kindles and iBooks are killing jobs by
    destroying the paper book industry. I see it that they have create a way
    to produce books for less than a third the cost without destroying forests
    and emitting greenhouse gases in the process. They have created wealth.
    They are encouraging literacy and cutting the costs of education.
    Roedy Green, May 6, 2011
    #1
    1. Advertising

  2. Roedy Green

    Jim Janney Guest

    Roedy Green <> writes:

    > On Thu, 05 May 2011 12:21:55 -0700, Zapanaz
    > <http://joecosby.com/code/> wrote, quoted or indirectly
    > quoted someone who said :
    >
    >>The problem is, where I'm creating sortedMap above, I need the map to
    >>retain the insertion order. If what's returned actually is a Map,
    >>rather than a LinkedHashMap, then the results the user actually sees
    >>are going to be in the wrong order. Making things worse, in this case,
    >>nothing would actually break, only the end user would notice anything
    >>was actually wrong.

    >
    > The idea of an interface is you should be able to substitute some
    > other implementation and all should work unmodified. That is not the
    > case with the interface you chose. You need to either find or create
    > an interface that nails down all the desired behaviour, or use the
    > original actual implementation.
    >
    > I am not a big fan of this pattern for the following reasons:
    > 1. it slows down access.
    > 2. it obfuscates just what the code is using NOW for the
    > implementation.
    > 3. It is more verbose, giving you one more thing you have to eyeball
    > carefully and make dance through the genericity hoops. That makes for
    > buggier code.
    >
    > The main place it would pay off is if you plan to later do some fine
    > tuning, and want to leave open the option of easily substituting your
    > own tuned collection classes.


    Wandering off the original topic, this might interest you if you haven't
    already seen it, on the subject of deciding when to break rules:

    http://www.miller-mccune.com/culture-society/triumph-of-the-cyborg-composer-8507

    --
    Jim Janney
    Jim Janney, May 6, 2011
    #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. Jim Janney
    Replies:
    43
    Views:
    1,003
    Arved Sandstrom
    May 15, 2011
  2. Steven Simpson
    Replies:
    5
    Views:
    327
  3. Michal Kleczek
    Replies:
    10
    Views:
    509
    Arved Sandstrom
    May 10, 2011
  4. Pallav singh
    Replies:
    0
    Views:
    338
    Pallav singh
    Jan 22, 2012
  5. Pallav singh
    Replies:
    0
    Views:
    384
    Pallav singh
    Jan 22, 2012
Loading...

Share This Page