Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!weretis.net!feeder4.news.weretis.net!newsfeed.utanet.at!newscore.univie.ac.at!aconews-feed.univie.ac.at!aconews.univie.ac.at!not-for-mail Newsgroups: comp.lang.java.programmer From: Andreas Leitgeb Subject: Re: calling own methods from constructor References: Reply-To: avl@logic.at User-Agent: slrn/pre0.9.9-111 (Linux) Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: Date: 07 Apr 2011 08:36:49 GMT Lines: 42 NNTP-Posting-Host: gamma.logic.tuwien.ac.at X-Trace: 1302165409 tunews.univie.ac.at 60386 128.130.175.3 X-Complaints-To: abuse@tuwien.ac.at Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:2915 Tom Anderson wrote: > On Wed, 6 Apr 2011, Andreas Leitgeb wrote: >> Is there any *good* use of having the constructor call a method that >> actually *can* be overridden in a subclass? I mean, are there >> (non-anti)patterns of explicitly allowing subclasses to hook into >> base-class's construction? > public abstract class Library { > private List documents; > protected Library() { > documents = new ArrayList(); > Collection titles = listDocuments(); > for (String title: titles) { > Document doc = loadDocument(title); > // do other preparatory stuff with the document > documents.add(doc); > } > } > protected abstract Collection listDocuments(); > protected abstract Document loadDocument(String title); > } > > public class FilesystemLibrary extends Library { > // ... > } > [...] Sorry, in my eyes, this is one of the anti-patterns. > What are the alternatives? It's hard to speculate about alternatives for an artificial example. I'd say, that they exist for any reasonable specification of the problem, and "back it up"(*) by declaring specifications for which no (or only mindboggingly contrived) alternatives exist as unreasonable ;-) (*): In German, we use double-quotes also to indicate tongue-in-cheek formulations that aren't to be taken entirely literally/serious. I've recently learned that they are not always thusly understood, elsewhere. Therefore this explanation. Is there any common markup for it that would be recognized in the English-speaking world?