I believe that when you create an object using something like Object object = new Object() you are creating a variable with a name (on the left of the equals sign) and creating the actual object itself (on the right) by calling the constructor. When you do something like Object object = Object.method() you are doing the same thing except that the object being created (on the right) isn't created by calling a constructor but one is being returned for you as the result of the method. I think sometimes they are called factory methods.
Well, no, not every method which happens to return an object is a factory method. Consider for example an ordinary getter method (public Date getDate()) which gets a Date attribute from some object. You wouldn't call that a factory method, would you?
I think you would only call it a factory method if you knew that the method was actually creating the object it was going to return. Of course that's just a brief and incomplete description -- have a look at the Wikipedia article Factory method pattern for much, much, much more information about the pattern.
Paul Clapham wrote:Well, no, not every method which happens to return an object is a factory method. Consider for example an ordinary getter method (public Date getDate()) which gets a Date attribute from some object. You wouldn't call that a factory method, would you?
I think you would only call it a factory method if you knew that the method was actually creating the object it was going to return. Of course that's just a brief and incomplete description -- have a look at the Wikipedia article Factory method pattern for much, much, much more information about the pattern.
Well, the method he is showing is a static method. (It is called with class name)
And static method returning the instance of the class in which it is defined is called factory method (As far as I know)
So, public Date getDate() will be called on an object..
Now had this method been static and in Date class.. This would be factory method.. (That is the case here.. GraphicsEnvironment static method returning instance of that class will always be factory method))
Well, yes, it's true that a static method which returns an object is definitely more likely to be a factory method. I'd agree with that. But not 100% of them, I don't think.
Of course it's possible for a bad coder to write a bad application in which every method is static. Those methods aren't likely to be factory methods. But let's not pay any attention to those, let's stick to good code. I had a quick look through the Java API and came up with BigDecimal.valueOf(long), which returns a BigDecimal object. I don't think that's a factory method, at least not according to the Wikipedia article which I linked to. I'm sure there are lots of others, but the API doesn't use many static methods so it's time-consuming to review them.
Paul Clapham wrote:I had a quick look through the Java API and came up with BigDecimal.valueOf(long), which returns a BigDecimal object. I don't think that's a factory method, at least not according to the Wikipedia article which I linked to.
Yeah, that's not a factory method, and by reading that article, it got almost clear except one thing..
I came across a statement there: -
Factory methods encapsulate the creation of objects (The first line under the topic Encapsulation)
And I don't exactly understand the purpose of this statement as being used as an explanation of the concept..
I am saying this because, if you take a look at the java code given just above that topic.. (Complex class one..)
It has two methods, and isn't those methods following the above statement?? (So, exactly what is the difference between a Factory method and a static Factory method?)
Campbell Ritchie wrote:I cannot imagine writing a factory method and not making it static. For more discussion look here amd here, and find the book mentioned.
Sir you are right by saying that. But I was referring to the FactoryMethodPattern link that Paul gave me, and there I say this statement that says: -
"The code above is not a factory method pattern (we can not override static), but rather an example of static factory method"
This statement is given just below the first Java class (Complex class) on that link..
So, what I want to ask is, "Is that statement referring to Factory Method and Static Factory Method as two different concepts??"
I'll appreciate your response