Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.glorb.com!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe05.iad.POSTED!8ad76e89!not-for-mail From: Arved Sandstrom User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.17) Gecko/20110424 Lightning/1.0b2 Thunderbird/3.1.10 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: "Program to an interface" - When to break a design pattern References: <9dt5s6dalhetgfe99qs92c02hf0dbas44e@4ax.com> <2psjssq4zj.fsf@shell.xmission.com> <2poc3gq3p2.fsf@shell.xmission.com> <2pk4e4q2sq.fsf@shell.xmission.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Lines: 31 Message-ID: X-Complaints-To: abuse@newsgroups-download.com NNTP-Posting-Date: Fri, 06 May 2011 20:30:15 UTC Organization: Public Usenet Newsgroup Access Date: Fri, 06 May 2011 17:30:14 -0300 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3712 On 11-05-06 11:03 AM, Patricia Shanahan wrote: > On 5/6/2011 3:24 AM, Arved Sandstrom wrote: > ... >> As far as your nuance is concerned, I'm interested (the _OP_ is >> interested) in providing a map with predictable insertion order. Map >> does not satisfy that; LinkedHashMap does. If I return Map then I am >> making a mistake. > ... > > I think this is the key point. > > Compare the following two rules: > > 1. Program to an interface. > > 2. Program to the least specific type that meets the requirements, with > preference for interfaces. > > Much of the time, the two rules give the same result. They differ in > cases like this one, where there is no interface that meets the > requirements. The second rule also helps with deciding which interface > to use. If you only need a Set, don't specify a SortedSet. > > Patricia I agree wholeheartedly. #2 is the real rule. Prefer interfaces. In some cases even consider writing your own interface and some adapters so that a bunch of calling code sees an interface. But from time to time the least specific type that meets the requirements is a concrete class. AHS