Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!news-1.dfn.de!news.dfn.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: a question about factory method Date: Fri, 19 Dec 2014 01:39:25 +0100 Lines: 32 Message-ID: References: <74e17e36-3ccb-45c4-b50a-439d2799e3c7@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net bjpaTlD9WHx7T/VnMZipvAv3Zvuu/MpCU9dcizqvcRK9zGmh0= Cancel-Lock: sha1:FasZ8RyXL61rGx9hhY5ESGTixSE= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 In-Reply-To: Xref: csiph.com comp.lang.java.programmer:30591 On 18.12.2014 20:24, Eric Sosman wrote: > As to *how* a factory method might choose which of several classes > to return, you already illustrated one way (paraphrased): > > public static Calendar getInstance(Date aDate, int choice) { > if (choice == 1) { > ... //return a GregorianCalendar object > } else if (choice == 2) { > ... //return a MayanCalendar object > } else if (choice == 3) { > ... //return a JulianCalendar object > } else { > return null; > } > } > > To me, this doesn't seem a huge advantage: The caller who writes > `getInstance(date, 2)' could just as easily write `new MayanCalendar()' > with about the same effect. The crucial point is that the caller does not need to know the criteria when to create what type. That _is_ a huge advantage. Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/