Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #30588

Re: a question about factory method

Path csiph.com!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: a question about factory method
Date Thu, 18 Dec 2014 14:24:06 -0500
Organization A noiseless patient Spider
Lines 63
Message-ID <m6v9k5$5fb$1@dont-email.me> (permalink)
References <ff7f0941-582f-4a6f-83fd-2fa71434ff46@googlegroups.com> <m6usaq$6bg$1@dont-email.me> <ae5dcc85-b23a-4ee3-94b3-350e585f502d@googlegroups.com> <m6v1e2$u2d$1@dont-email.me> <74e17e36-3ccb-45c4-b50a-439d2799e3c7@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Thu, 18 Dec 2014 19:23:49 +0000 (UTC)
Injection-Info mx02.eternal-september.org; posting-host="bd4e1cba8d8592bec5d5ed9e4537fbfd"; logging-data="5611"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18C95dPnOpKbDZMnaO1Eu9l"
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0
In-Reply-To <74e17e36-3ccb-45c4-b50a-439d2799e3c7@googlegroups.com>
Cancel-Lock sha1:KDZ0UdM0LpXcziScUh+k4v9OovU=
Xref csiph.com comp.lang.java.programmer:30588

Show key headers only | View raw


On 12/18/2014 1:43 PM, John wrote:
> Eric,
>
> Your reply goes off the topic. I didn't mean digging deep into Calendar thing. Since you mentioned it, I use it as an example to ask.
>
> I meant to ask How to use factory method to return different objects(which belong to the same parent class). A couple of repliers, including you, mentioned that as one of several advantages of using factory versus using a public constructor.

     Sorry for misunderstanding the thrust of your question.

     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.  So let's think of another possibility
(again, this is largely made-up): The switch from the Julian to the
Gregorian calendar happened at different times in different parts of
the world, so the particular kind of Calendar to use is a function
of both the Date and the Locale.  We might write something like:

	public static Calendar getInstance(Date date, Locale locale) {
	    Date cutover = ...; // locale's Gregorian adoption date
	    if (date.compareTo(cutover) < 0) {
	        return new JulianCalendar(date);
	    } else {
	        return new GregorianCalendar(date);
	    }
	}

The caller needn't worry about determining `cutover' (it's probably not
a one-liner!), he just gets the "right kind" of Calendar for the Date
in question at the specified Locale.

     Approaches like this, though, require the factory method to have an
enumeration of all the possibilities, built right into the Java source.
Another possibility is often used in "provider" or "plugin" styles: The
factory method consults an environment variable or a configuration file
or a database or some such, and gets the name of a suitable class -- as
a String.  Then it can use Class.forName() to load the named class, and
can create an instance by invoking the class' constructor reflectively.
This style requires more effort to set up, but is much more flexible: To
introduce a new class you needn't change a single line of the factory
method or its caller; you write your new class, put it in a .jar (most
likely), and change the environment variable or config file.  This is
(more or less) how Swing loads pluggable look-and-feel implementations,
and similar approaches are used for things like externally-supplied
cryptographic suites.

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

a question about factory method John <xsli2@yahoo.com> - 2014-12-18 05:57 -0800
  Re: a question about factory method Robert Klemme <shortcutter@googlemail.com> - 2014-12-18 15:28 +0100
  Re: a question about factory method Silvio <silvio@internet.com> - 2014-12-18 15:43 +0100
  Re: a question about factory method Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-12-18 10:37 -0500
    Re: a question about factory method John <xsli2@yahoo.com> - 2014-12-18 08:04 -0800
      Re: a question about factory method Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-12-18 12:04 -0500
        Re: a question about factory method John <xsli2@yahoo.com> - 2014-12-18 10:43 -0800
          Re: a question about factory method Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-12-18 14:24 -0500
            Re: a question about factory method Robert Klemme <shortcutter@googlemail.com> - 2014-12-19 01:39 +0100
  Re: a question about factory method Roedy Green <see_website@mindprod.com.invalid> - 2014-12-18 13:43 -0800

csiph-web