• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

factory methods???????

 
Arijit Daripa
Ranch Hand
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One question, I found in Master Exam, asking about classes instantiated by factory methods.
Anyone please, please define factory method.
 
Brij Garg
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Test
{
private Test t = new Test();
private Test(){}
public static Test getInstance()
{
return t;
}
}

In this example getInstance is factory method.
factory method is just a fancy name. Any method which create or return object of the class can be factory method.

Please let me know If I am wrong somewhere.
 
Arijit Daripa
Ranch Hand
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brij garg:
Any method which create or return object of the class can be factory method.

Thanks brij. I will remember the above quote.
 
chander shivdasani
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Factory methods are methods which creates instance of a particular class.
 
Arijit Daripa
Ranch Hand
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chander....
Thanks a lot....
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Factory methods are not part of the exam. To add to that the Factory pattern by itself is split into the Abstract Factory pattern and the Factory method pattern. The details however are not needed for the SCJP
 
Arijit Daripa
Ranch Hand
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Deepak Bala:
Factory methods are not part of the exam. To add to that the Factory pattern by itself is split into the Abstract Factory pattern and the Factory method pattern. The details however are not needed for the SCJP


But there was a question in Master Exam, asking, "Which of the following classes are instantiated by factory methods?" That's why I asked the definition.
Well... Thanks for the information
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Brij garg your code has a mistake, it will not compile as you are accessing a non-static field from a static method.



@arijit, you can understand this as follows

If you want that no one can create an instance of your class, and you provide a method that provides instances of that class, that method is called factory method. So basically a factory method returns an instance of its own class.

To do this you will have to create all the constructors of the class private and have to create a static public method, that returns an instance of that class. The method might return new instance every time or might return the same instance all the time as in the example by brij.

There are many reasons for this. Your class might be incomplete in itself like the Calendar class, which has a getInstance method which returns an instance of GregorianCalendar class usually. Other reason might be that you want that only one instance of the class be created on one JVM as shown in the code by brij...
 
Arijit Daripa
Ranch Hand
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit.......
That mean every getInstance() method is a factory method.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic