Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Daniele Futtorovic Newsgroups: comp.lang.java.programmer Subject: Re: "Program to an interface" - When to break a design pattern Date: Thu, 05 May 2011 21:47:56 +0200 Organization: A noiseless patient Spider Lines: 69 Message-ID: References: <9dt5s6dalhetgfe99qs92c02hf0dbas44e@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 5 May 2011 19:47:53 +0000 (UTC) Injection-Info: mx03.eternal-september.org; posting-host="3L0fosuCYLsM9cW5CzHNyA"; logging-data="10166"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/JEWOOoOT6GQTBA8xgp5Dz" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10 In-Reply-To: <9dt5s6dalhetgfe99qs92c02hf0dbas44e@4ax.com> Cancel-Lock: sha1:28dg1GXngre+DOFwV4TB9oF6X60= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3582 On 05/05/2011 21:21, Zapanaz allegedly wrote: > > I've seen this design pattern before > > http://witte-consulting.com/documents/design-principles/ > > and, in general, I see the point of it. > > But say we've got something like this > > LinkedHashMap sortedMap = this.getSortedMap(); > > So you have the method > > public LinkedHashMap getSortedMap() { > //do stuff > } > > (not necessarily public) > > Now the design principle says, the method signature should instead be > > public Map getSortedMap() { > //do stuff > } > > 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. > > So in this case, it seems to me, that using LinkedHashMap in the > method signature makes sense. The fact that the return retains the > insertion order is an integral part of what the method does. > > If nothing else, it's going to save Fred Developer down the line from > looking at the code around this > > Map sortedMap = this.getSortedMap(); > > and thinking "wait, how do I know getSortedMap() is going to return a > result with the right ordering?", and having to waste time digging > into that method. > > *** > Opinions? Angry mob with torches and pitchforks? The Spanish > Inquisition? Prodding with a cushion: not your goddamn business. As far as the bit of code you showed is concerned, "the right ordering" is whatever you get passed. If you take input somewhere and need to retain passing order, then *your code* ought to work with a LinkedHashMap. But as for what you get passed, that's the responsibility of the passer -- within certain boundaries, of course: if it's invalid in some way, throw an Exception. Incidentally, if it's a sorted map, you might as well declare it as a java.util.SortedMap. Which has an ordering, although it's not the same thing as a LinkedHashMap, of course. -- DF. An escaped convict once said to me: "Alcatraz is the place to be"