Good day!
I am a little bit confused. I want to use a calendar so I searched it in the internet and encountered the following code:
Ca1endar c = Calendar.getlnstance();
c.set(2011,2, 5,1,25);
But I learned that this is a static method:
Calendar.getlnstance();
How come I can get the Instance of the Calendar(Abstract Class) if the method called is static?
I really want to understand it. So next time I can a开发者_JS百科lso create a Static method that can create an instance.
Thank you.
It's the static factory method. The idea is the method is the one calling the constructor and it returns the constructed object.
The body of Calendar.getInstance()
is perhaps something like this:
return new SomeCalendar(now);
where SomeCalender
is an concrete implementation of the abstract class Calendar
. Some of the advantages are: you don't have to care about the underlying class (as far as you know it's just a Calendar), and the underlying implementation can change without affecting you (for example, the code can be changed to return new AnotherCalendar()
and you don't have to change anything in your code)
Since it is a static method, you can call it on the type itself (Calendar.getInstance();
) as opposed to an instance of that type (Calendar c = ...; c.getInstance();
).
An instance method requires that you already have an instance to call the method on.
Being static does not mean that you can't take instances as a parameter, return an instance as a result or create an instance. It just means that the method can be called without first having an instance, and that in the method there is no such thing as this
.
You generally wouldn't want this sort of factory method to be an instance method (non-static) because it would mean you'd need to already have an instance to create a new one. How would you create the first one?
(Non-static factory methods do exist, but they are more commonly used for creating a different type of object. For example, a Builder or Factory class will typically have a method that creates instances of some other class.)
Java does this sort of thing all over the place. Look at the XML API.
This is a way to maintain a single interface into a Calendar
system even if the underlying implementation is changed. Yes the method is a factory method, however, the point some of the other answers seem to miss is that it can return different implementations of Calendar
.
That is how Calendar can be returned even if it is abstract. A overly simplified implementation of getInstance()
could be
Calendar getInstance()
{
return new GregorianCalendar();
}
The caller of getInstance()
does not need to know the implementation type in order to get the current date and time.
I recommend looking through OpenJDK
The fact the factory method is static does not infer that the Calendar is static - it just provides an access mechanism to create a Calendar.
Check out this post about how to use this pattern yourself: http://today.java.net/pub/a/today/2005/03/09/factory.html
It also explains a bit more about the principles behind the pattern.
I second the recommendation to read Effective Java though - it's a great book despite it's age!
Its a alternative way to create a new object in java. Traditionally constructors are the way to go, but since there are some advantages for using a static factory, its preferred over plain vanilla constructors.
You should really pick up Effective Java by Joshua Bloch. Its the first item in the book. Preferring static factory for contructor. The advantages he mentions are:
1_ Since static factory have names that could be descriptive.
2_ Then can be made to return any subtype, not just the type of the particular class.
3_ You can handle concepts like caching and singletons with in a factory method. (you do not have to blindly create new object).
4_ To reduce the initialization code. For instance instead of List newList = new ArrayList, we can have a method like getStringList();
The fourth advantage is something I am not very particular about, but I see Google Guava framework has implemented it.Please pick up the book. Its one of the best programming book for java.
精彩评论