Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Wed, 11 May 2011 12:41:49 -0500 Date: Wed, 11 May 2011 10:41:49 -0700 From: Patricia Shanahan User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.9.2.17) Gecko/20110414 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> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Lines: 38 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 75.8.126.96 X-Trace: sv3-7ZPjjufJ6qPb3YYTMoEJZGNMu+xa77pHixVuoBPM3i3S2pLHYLH5zEaglky3/00Xw+B7xyKlpOoiK5I!gHMN+yjYhxQPveZpgPxztgvDa1NUS/zclsFDsz8lDiUSqM6aQRlhxZDO2ATQkAzF99KA6l6BdBpA!o4oMXpU4zKfdPgLu/b7zee24C0thzKSZxEKCy9XeMUY= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 3421 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3981 On 5/10/2011 4:01 PM, Arved Sandstrom wrote: ... > I agree that _declaring_ the return type as LinkedHashMap does not > achieve that; the ironclad knowledge that it _is_ LinkedHashMap does, > even if it's declared as Map. But in some cases (see above) there is no > harm, and arguably some utility, in making the explicit declaration. > That's the point I've been trying to make. Evidently I'm running into > the "program to an interface at all costs" principle. ... I think some of the trouble might lie in the ambiguity, in a Java context, of the sentence "Program to an interface.". Meaning 1: Users of some module (in the widest sense) should depend only on what is declared and specified about the module, not on direct knowledge of how it is currently implemented. Meaning 2: Java programs should depend only in Java interface types to represent what is declared and specified about a module for the use of its callers. I am very strongly in favor of meaning 1 of "Program to an interface.". Everything any user of a module needs to know about it should be declared and/or specified as part of its interface (in the general sense), and using code should depend only on what is declared and specified. Failure to follow this principle can lead to fragile code in which a change to the implementation of one module that does not change any part of its declaration or specification breaks something else. At the worst, nobody dares change anything. I like Java interface types as a way of abstracting out interfaces between modules, where appropriate, but I do not think "Program to an interface" should be considered anywhere near as absolute a principle in meaning 2 as in meaning 1. Patricia