Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #3819

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

From Arved Sandstrom <asandstrom3minus1@eastlink.ca>
Newsgroups comp.lang.java.programmer
Subject Re: "Program to an interface" - When to break a design pattern
References (6 earlier) <iq17dt$c54$1@dont-email.me> <c3lbs6pc00he19u3ru6asfpdicu552pvu0@4ax.com> <iq4rj8$j2j$1@dont-email.me> <JLnxp.68858$yp3.49687@newsfe09.iad> <iq68h6$7og$1@dont-email.me>
Message-ID <whAxp.19939$Ot6.2301@newsfe15.iad> (permalink)
Organization Public Usenet Newsgroup Access
Date 2011-05-08 14:20 -0300

Show all headers | View raw


On 11-05-08 11:15 AM, Daniele Futtorovic wrote:
> On 08/05/2011 05:05, Arved Sandstrom allegedly wrote:
>> On 11-05-07 10:28 PM, Daniele Futtorovic wrote:
>>> On 08/05/2011 01:26, Zapanaz allegedly wrote:
>>>> On Fri, 06 May 2011 18:25:31 +0200, Daniele Futtorovic
>>>> <da.futt.news@laposte-dot-net.invalid>   wrote:
>>>>
>>>>> On 06/05/2011 08:25, Zapanaz allegedly wrote:
>>>>>> On Thu, 05 May 2011 22:48:21 +0200, Daniele Futtorovic
>>>>>> <da.futt.news@laposte-dot-net.invalid>    wrote:
>>>>>>
>>>>>>> But the code that *calls*
>>>>>>> getSortedMap() doesn't have to care about whether or not it's a
>>>>>>> LinkedHashMap
>>>>>>
>>>>>> yes, it does.
>>>>>>
>>>>>> The code that calls getSortedMap() has to know whether the results
>>>>>> will be sorted or not.
>>>>>
>>>>> Not unless the calling code inserts in that Map *and iterates* over
>>>>> it,
>>>>> it doesn't.
>>>>>
>>>>> A LinkedHashMap is not sorted. At least not in the absolute sense.
>>>>> It is
>>>>> sorted relatively to another sorting, viz. it (possibly) replicates
>>>>> the
>>>>> iteration order of another Map. But as I have tried to explain before,
>>>>> knowing that it's a LinkedHashMap *without knowing in what order the
>>>>> insertion took place* doesn't tell you *anything* about the iteration
>>>>> order.
>>>>
>>>> I am using the word "sorted" in an incorrect way, it's true.
>>>>
>>>> This is from a real-world application, the results in the map are
>>>> presented in a UI, and they do in fact have to be in a particular
>>>> order in that UI. They are ordered by an algorithm based on a set of
>>>> user-entered choices, and inserted in that order. So if that order
>>>> isn't preserved in the map, the way the items appear in the UI would
>>>> not be what the user had selected.
>>>
>>> This whole discussion is about the declared type, not the runtime type.
>>
>> Sort of helps if the declared type has some reasonable relationship to
>> the possible runtime types, actually. If the contract of the declared
>> type deviates too much from the contract of the possible runtime types,
>> which is the case here, what's the point?
>>
>>> Anyway, I've had enough of this.
>>>
>> Probably just as well. You can't convince everyone to do something
>> ill-advised.
> 
> Them's fighting words.

No, merely argumentative words. Like I said, Daniele, I respect your
opinion - you produce good posts and I read them carefully - but I am
not impressed by dismissive statements like "Anyway, I've had enough of
this". That translates to "whatever, I can't make these idiots see the
light, I'm washing my hands of them" in vernacular English. If you
happened to say that in a professional environment, say a developers'
meeting, you'd have a lot of pissed-off people.

It's actually a lot better to say something like "I believe I'm correct,
but I'm clearly not making my case; I'll leave the discussion at this
point". Lot more diplomatic.

> Show me a code example with a method, that is not a factory method; that
> returns a LinkedHashMap instance; for which it matters to the caller
> that the return type be declared as LinkedHashMap, rather than Map.
> 
I don't know why you're excluding factory methods. In fact a factory
method would be amongst the most likely to declare the return as Map and
not as LinkedHashMap, so I wouldn't be likely to use one as an example
anyway.

I don't think I need to invent a new example: the OP has already
provided one. Let's say he's got client class A (maybe a JSF backing
bean) that absolutely requires that the DataModel be loaded by a map
obeying this contract. I'll go further and say that if your requirements
are so tenuous and erratic that this specific requirement is going to
change a fair bit, that you've got bigger problems than how you declare
your collections types. From the sounds of what the OP said this is a
definite, solid, stable and essential requirement.

Provider class B with the method in question is tasked to supply the map
that obeys this "predictable iteration order" contract...namely a
LinkedHashMap.

You can absolutely declare that return value from the B.getMap() method
as a Map. You'll need to comprehensively Javadoc that method as a
result, even if it's not a public/published API method, because now Map
tells you very little. You'll also do well to comment the location of
the call so as to increase the likelihood that future maintenance in
class A doesn't ignore the special required nature of _that_ Map.

Or you could in this situation just return LinkedHashMap. By doing so
you now locked that relationship in...which given the fact that Java
possesses no interface-level capability of enforcing this behavioural
contract, gives you language-level assurance of using the right map.

I'd very possibly think differently about this if I had an inkling that
this method was to be made publicly available in a library to any third
party client. But that's not the impression I'm getting from the OP's
description.

AHS

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

"Program to an interface" - When to break a design pattern Zapanaz <http://joecosby.com/code/mail.pl@foo.com> - 2011-05-05 12:21 -0700
  Re: "Program to an interface" - When to break a design pattern Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-05-05 15:43 -0400
    Re: "Program to an interface" - When to break a design pattern Lew <noone@lewscanon.com> - 2011-05-05 17:19 -0400
  Re: "Program to an interface" - When to break a design pattern Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-05-05 21:47 +0200
  Re: "Program to an interface" - When to break a design pattern Jim Janney <jjanney@shell.xmission.com> - 2011-05-05 14:14 -0600
    Re: "Program to an interface" - When to break a design pattern Zapanaz <http://joecosby.com/code/mail.pl@foo.com> - 2011-05-05 13:26 -0700
    Re: "Program to an interface" - When to break a design pattern Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-05-05 22:27 +0200
      Re: "Program to an interface" - When to break a design pattern Jim Janney <jjanney@shell.xmission.com> - 2011-05-05 14:42 -0600
        Re: "Program to an interface" - When to break a design pattern Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-05-05 22:48 +0200
          Re: "Program to an interface" - When to break a design pattern Jim Janney <jjanney@shell.xmission.com> - 2011-05-05 15:02 -0600
            Re: "Program to an interface" - When to break a design pattern Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-05-06 00:02 +0200
              Re: "Program to an interface" - When to break a design pattern Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-05-05 19:49 -0300
                Re: "Program to an interface" - When to break a design pattern Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-05-06 02:28 +0200
                Re: "Program to an interface" - When to break a design pattern Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-05-06 07:24 -0300
                Re: "Program to an interface" - When to break a design pattern Patricia Shanahan <pats@acm.org> - 2011-05-06 07:03 -0700
                Re: "Program to an interface" - When to break a design pattern Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-05-06 17:30 -0300
                Re: "Program to an interface" - When to break a design pattern Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-05-06 18:56 +0200
                Re: "Program to an interface" - When to break a design pattern Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-05-06 17:50 -0300
                Re: "Program to an interface" - When to break a design pattern Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-05-06 23:37 +0200
                Re: "Program to an interface" - When to break a design pattern Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-05-06 19:43 -0300
              Re: "Program to an interface" - When to break a design pattern Jim Janney <jjanney@shell.xmission.com> - 2011-05-05 17:17 -0600
                Re: "Program to an interface" - When to break a design pattern Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-05-06 02:28 +0200
          Re: "Program to an interface" - When to break a design pattern Zapanaz <http://joecosby.com/code/mail.pl@foo.com> - 2011-05-05 23:25 -0700
            Re: "Program to an interface" - When to break a design pattern Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-05-06 18:25 +0200
              Re: "Program to an interface" - When to break a design pattern Zapanaz <http://joecosby.com/code/mail.pl@foo.com> - 2011-05-07 16:26 -0700
                Re: "Program to an interface" - When to break a design pattern Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-05-08 03:28 +0200
                Re: "Program to an interface" - When to break a design pattern Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-05-08 00:05 -0300
                Re: "Program to an interface" - When to break a design pattern Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-05-08 16:15 +0200
                Re: "Program to an interface" - When to break a design pattern Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-05-08 14:20 -0300
                Re: "Program to an interface" - When to break a design pattern Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-05-08 19:48 +0200
                Re: "Program to an interface" - When to break a design pattern markspace <-@.> - 2011-05-10 07:36 -0700
                Re: "Program to an interface" - When to break a design pattern Jim Janney <jjanney@shell.xmission.com> - 2011-05-10 13:04 -0600
                Re: "Program to an interface" - When to break a design pattern Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-05-10 21:31 +0200
                Re: "Program to an interface" - When to break a design pattern Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-05-10 20:01 -0300
                Re: "Program to an interface" - When to break a design pattern Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-05-11 19:14 +0200
                Re: "Program to an interface" - When to break a design pattern Patricia Shanahan <pats@acm.org> - 2011-05-11 10:41 -0700
                Re: "Program to an interface" - When to break a design pattern Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-05-11 19:55 +0200
                Re: "Program to an interface" - When to break a design pattern Lew <noone@lewscanon.com> - 2011-05-11 16:42 -0400
                Re: "Program to an interface" - When to break a design pattern Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-05-11 23:34 +0200
                Re: "Program to an interface" - When to break a design pattern "John B. Matthews" <nospam@nospam.invalid> - 2011-05-12 00:51 -0400
                Re: "Program to an interface" - When to break a design pattern Lew <noone@lewscanon.com> - 2011-05-12 00:58 -0400
                Re: "Program to an interface" - When to break a design pattern Tom Anderson <twic@urchin.earth.li> - 2011-05-12 20:08 +0100
                Re: "Program to an interface" - When to break a design pattern Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-05-15 13:25 -0300
        Re: "Program to an interface" - When to break a design pattern Lew <noone@lewscanon.com> - 2011-05-05 17:24 -0400
          Re: "Program to an interface" - When to break a design pattern Jim Janney <jjanney@shell.xmission.com> - 2011-05-05 16:00 -0600
          Re: "Program to an interface" - When to break a design pattern Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2011-05-06 15:01 +0300
            Re: "Program to an interface" - When to break a design pattern Lew <noone@lewscanon.com> - 2011-05-06 12:17 -0400
              Re: "Program to an interface" - When to break a design pattern Zapanaz <http://joecosby.com/code/mail.pl@foo.com> - 2011-05-07 16:28 -0700
    Re: "Program to an interface" - When to break a design pattern Lew <noone@lewscanon.com> - 2011-05-05 17:21 -0400
      Re: "Program to an interface" - When to break a design pattern Jim Janney <jjanney@shell.xmission.com> - 2011-05-05 15:58 -0600
        Re: "Program to an interface" - When to break a design pattern Lew <noone@lewscanon.com> - 2011-05-05 18:18 -0400
      Re: "Program to an interface" - When to break a design pattern Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-05-05 19:20 -0300
        Re: "Program to an interface" - When to break a design pattern Lew <noone@lewscanon.com> - 2011-05-05 18:23 -0400
          Re: "Program to an interface" - When to break a design pattern Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-05-05 20:17 -0300
  Re: "Program to an interface" - When to break a design pattern Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-05-05 18:26 -0300
  Re: "Program to an interface" - When to break a design pattern Steven Simpson <ss@domain.invalid> - 2011-05-05 22:57 +0100
    Re: "Program to an interface" - When to break a design pattern Tom Anderson <twic@urchin.earth.li> - 2011-05-05 23:29 +0100
      Re: "Program to an interface" - When to break a design pattern Steven Simpson <ss@domain.invalid> - 2011-05-06 13:30 +0100
        Re: "Program to an interface" - When to break a design pattern Lew <noone@lewscanon.com> - 2011-05-06 12:19 -0400
    Re: "Program to an interface" - When to break a design pattern Jim Janney <jjanney@shell.xmission.com> - 2011-05-05 16:41 -0600
      Re: "Program to an interface" - When to break a design pattern Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-05-05 20:47 -0300
  Re: "Program to an interface" - When to break a design pattern Roedy Green <see_website@mindprod.com.invalid> - 2011-05-05 16:41 -0700
    Re: "Program to an interface" - When to break a design pattern Jim Janney <jjanney@shell.xmission.com> - 2011-05-05 22:47 -0600
    Re: "Program to an interface" - When to break a design pattern Zapanaz <http://joecosby.com/code/mail.pl@foo.com> - 2011-05-05 23:28 -0700
  Re: "Program to an interface" - When to break a design pattern Michal Kleczek <kleku75@gmail.com> - 2011-05-06 17:15 +0200
    Re: "Program to an interface" - When to break a design pattern Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-05-06 20:53 -0300
      Re: "Program to an interface" - When to break a design pattern Lew <noone@lewscanon.com> - 2011-05-06 21:39 -0400
        Re: "Program to an interface" - When to break a design pattern Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-05-07 00:56 -0300
      Re: "Program to an interface" - When to break a design pattern Michal Kleczek <kleku75@gmail.com> - 2011-05-08 12:24 +0200
        Re: "Program to an interface" - When to break a design pattern Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-05-08 13:42 -0300
          Re: "Program to an interface" - When to break a design pattern Michal Kleczek <kleku75@gmail.com> - 2011-05-09 11:04 +0200
            Re: "Program to an interface" - When to break a design pattern Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-05-09 19:33 -0300
              Re: "Program to an interface" - When to break a design pattern Michal Kleczek <kleku75@gmail.com> - 2011-05-10 15:51 +0200
                Re: "Program to an interface" - When to break a design pattern Jim Janney <jjanney@shell.xmission.com> - 2011-05-10 13:15 -0600
                Re: "Program to an interface" - When to break a design pattern Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-05-10 19:40 -0300

csiph-web