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


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

Re: a question about factory method

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: a question about factory method
Date 2014-12-18 12:04 -0500
Organization A noiseless patient Spider
Message-ID <m6v1e2$u2d$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>

Show all headers | View raw


On 12/18/2014 11:04 AM, John wrote:
> Thank you all for replies.
>
> Can you elaborate a little more about
>   Its getInstance() methods usually return a GregorianCalendar instance,
> but could (if Locales and so on were set up) return an instance of
> MayanCalendar or JulianCalendar or BabylonianCalendar or whatever:
> Classes whose names, whose very existence, might not have been known
> when the calling code was written.
>
> My guess is something like below. Is that correct? Thank you very much.
>
> public class Calendar {
>
>    /**
>    * Static factory method returns an object of this class or subclass. for choice: 1 for GregorianCalendar, 2 for MayanCalendar , 3 for JulianCalendar
>    */
>    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;
>      }
>
>    }
>
> ...
> }
>
> How can I make it return MayanCalendar or JulianCalendar?

     First, you'd need to write MayanCalendar and JulianCalendar
classes (they're not part of Java SE; I just made them up as
examples that someone might add someday).  These new classes would
be subclasses of Calendar:

	public class MayanCalendar extends Calendar { ... }
	public class JulianCalendar extends Calendar { ... }

... so that Calendar.getInstance() -- which must return a Calendar --
could legitimately return them.

     Next, you'd have to make the new classes known to Java.  I haven't
actually tried to do this, but it can (probably) be done somehow:  All
the variants of Calendar.getInstance() eventually wind up with a
TimeZone and a Locale, and from these they're supposed to produce a
suitable Calendar.  Java doesn't specify exactly how suitable Calendars
are found, but if you look at the source code for Calendar.java you'll
find that (in Sun/Oracle's implementation) the heavy lifting happens in
CalendarProvider and LocaleProviderAdapter classes.  (These are not part
of Java SE, but part of Sun/Oracle's implementation of Java SE.)

     I can't explore much further, because the JDK doesn't provide source
for those internal classes; all I can see is the compiled bytecode.  But
I imagine there's a configuration file somewhere that describes the
available Locales and things, and (presumably) there's a way to say
"For the following Locale, use *this* Calendar subclass."  Whether this
actually happens exactly this way with Calendar isn't crucial to the
issue of using a factory method; the point is that a factory method
*could* do something like this, and make a run-time choice of which
precise class it returns.  A constructor couldn't.

-- 
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