File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Factory Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Factory Methods" Watch "Factory Methods" New topic
Author

Factory Methods

Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

Taken from quiz A in the bates book, question 28.

Which classes are always instantiated via fatcory methods?

A. java.util.Date
B. java.util.Locale
C. java.util.Calender
D. java.text.DateFormat
E. java.text.NumberFormat

The correct answers are given as C, D and E.

Can you please explain what a factory method is and then why it applies to these three Classes and not Date or Locale?

Thanks.


OCPJP 6, OCMJD
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Hi, when you take a look at the Java 6 API documentation online,
Calender, DateFormat, NumberFormat, they are all abstract classes. You can instantiate any object from them.

In order to create an instance , we have to do this :
DateFormat df = DateFormat.getInstance();

The DateFormat df is actually a SimpleDateFormat object, according to the open source.

For Calender, we can call the getInstance() method to create a Gregorian calender object.
For NumberFormat, we can call the getInstance() method to create a ChoiceFormat or a DecimalFormat.

I think there may be a possibility that more different subclasses maybe developed for Calender, NumberFormat and DateFormat. That is why the getInstance method is needed to create different subclasses.

The only function of the getInstance method is to create objects. This is a software design issue that won't be on the exam. The design issue is about separating object creation and implementation. Briefly, when you design a class, you have a method to create instances of this class and you will have another method for other implementation. For example, if you have a Horse class, you have a getInstance() method to create one and a eat() method to implement eating.
Mahesh Murugaiyan
Greenhorn

Joined: Jun 25, 2009
Posts: 21
Hi Glen,

Calendar, DateFormat, NumberFormat are all Abstract classes.

In Java terms, we cannot instantiate any abstract class. Though they serve specific purpose, they are better when refined further by their sub classes. The subclasses of these classes add more functionality. For example, the Calendar.getInstance would give you a Gregorian Calendar, DateFormat.getInstance would give you a SimpleDateFormat.

Both GregorianCalendar, SimpleDateFormat extends their abstract class, and provide more value add.

About the factory method:

1. A Factory Method is a design pattern. Simply put, a factory method will abstract all the complexities involved in instantiating and initializing a object. In this case, while using Calendar.getInstance, we have no idea about how the object was created and presented back to you! you don't know what are the arguments passed, what default value to set etc.

2. Apart from just hiding the complexities, another cool aspect is, if the same object needs to be created in multiple places, then you can avoid having the initialization code every where. Its more clearer than having a private method constructing the object. By moving this piece of code into its own class, your class can worry about the actual business than about creating/initializing bits and pieces.

Cheers
Mahesh
Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

Great! Thats a clear explaination. Thank you Mahesh Murugaiyan
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3530
    
  15
Mahesh Murugaiyan wrote:For example, the Calendar.getInstance would give you a Gregorian Calendar

That's not completely true and what actually happens is a good demonstration of why the Calendar class is implemented as a factory. If you look at the source code for the Calendar.getInstance() method you will see it calls the createCalendar method and that decides what type of Calendar subclass to return based on the locale. In other words you can use the same code wherever your program is run and an instance of the appropriate class will be returned.


Joanne
Mahesh Murugaiyan
Greenhorn

Joined: Jun 25, 2009
Posts: 21
That's not completely true and what actually happens is a good demonstration of why the Calendar class is implemented as a factory. If you look at the source code for the Calendar.getInstance() method you will see it calls the createCalendar method and that decides what type of Calendar subclass to return based on the locale. In other words you can use the same code wherever your program is run and an instance of the appropriate class will be returned


Hello, The calendar.getInstance, if no locale is passed, and if the locale is not Japan, by default returns a Gregorian Calendar. Have a look at the code snippet and its output below:



The above code outputs:

java.util.GregorianCalendar[time=1329329597419,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/Los_Angeles",offset=-28800000,
dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=America/Los_Angeles,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,
startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],
firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2012,MONTH=1,WEEK_OF_YEAR=7,WEEK_OF_MONTH=3,DAY_OF_MONTH=15,DAY_OF_YEAR=46,DAY_OF_WEEK=4,
DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=10,HOUR_OF_DAY=10,MINUTE=13,SECOND=17,MILLISECOND=419,ZONE_OFFSET=-28800000,DST_OFFSET=0]


The Calendar.getInstance() indeed works as you said. Gets the Locale, and returns a specific subclass implementation based on Locale.

The getInstance() method abstracts all this locale checking and subclass creation from the end user which is the objective of a factory method!

Cheers
Mahesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Factory Methods