Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: "Program to an interface" - When to break a design pattern Date: Thu, 05 May 2011 17:19:23 -0400 Organization: albasani.net Lines: 37 Message-ID: References: <9dt5s6dalhetgfe99qs92c02hf0dbas44e@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net G+bmKfoojwFYtKXTIGLGHmRFQtUoLUf4/xutCOtD6Dq/5aT+EV3sC3TaE0MeCdbru+lo/IHlSlic7IOGGFGFiQ== NNTP-Posting-Date: Thu, 5 May 2011 21:19:22 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="+YME8IdeUqhVE4gK0vo8do6ZlQPpSBwnPZtL2UdCSD8d67z4LPQiQmF752XMEbB7RNDbjIEEcn33fu7RCNIFEu2dZfmFteC4rU7SL0RtXI1LRucKH1Uhg1zSwwOkBxXe"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.14) Gecko/20110223 Thunderbird/3.1.8 In-Reply-To: Cancel-Lock: sha1:kTvEkBWk19g9ZYjJh9vuRzfwxBc= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3597 Joshua Cranmer wrote: > Zapanaz wrote: >> 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, then you've declared the wrong type, duhh. Did you read the link you cited? >> 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. > > It's still the same map, whether you return it as a Map or as a LinkedHashMap. > That is, the same iterator will still be called, and will still return the > elements in the same order independent of how it was called. > > If you're worried that a later developer might not see that it is in the > proper order, you can include it in the documentation. Or perhaps return > SortedMap instead ;-). > If the need is for a sorted map, then the design principle mandates that the declared type be 'SortedMap'. Period. Declaring the variable as 'Map' could lead to troubles, e.g., accepting an assignment to point to an unsorted map. This would be the very same prinicple the OP cited, BTW, not some odd variation thereof. The notion that the principle calls for the declared value to be 'Map' when it should be 'SortedMap' is just a stupid misinterpretation of the principle. Programmers are supposed to be smart and to think, not to be stupid and superstitious. -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg