• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Static Factory Methods

 
Ranch Hand
Posts: 87
IntelliJ IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Can anyone help me on how to create Static factory Methods?

I know that Calendar and DateFormat class from Java API use Static Factory implementation.

But I am not really sure how implementation should go as I have read that Static Factory implementation avoids creation of objects multiple times.
How to really achieve that.


So can you please explain with an example.


Thanks,
Sumukh.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sumukh Deshpande wrote: I have read that Static Factory implementation avoids creation of objects multiple times.


Not really!. actually it depends how you implement the static factory method. singleton class uses this pattern.
example:
this method produce only one instance


this method produce multiple one instance


hope this helps
 
Sumukh Deshpande
Ranch Hand
Posts: 87
IntelliJ IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Seetharaman Venkatasamy.
Got it now.

But I have one more doubt, how does this is a better implementation than standard constructor provider by a class?

Is it that I don't need to refer the API as I am directly using a getInstance() method?

And does it have any relation with thread-safety?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sumukh Deshpande wrote:
does it have any relation with thread-safety?


Yes, It will allow you to create an object in the synchronized way with synchronized key word.


typically, static factory method has one advantage over *new Something()* expression, that is expressiveness.

i.e, *getInstance* is more meaningfull than *new Something* .
 
Sumukh Deshpande
Ranch Hand
Posts: 87
IntelliJ IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Seetharaman Venkatasamy I am clear now.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sumukh Deshpande wrote:Thanks Seetharaman Venkatasamy I am clear now.


You are welcome
 
Saloon Keeper
Posts: 15725
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like to use factory methods for value classes. If I have an immutable value class, I provide a factory method and make the constructor private. Most of the time, the factory method will simply call the constructor, but it gives me the freedom to return cached values to the user in the future.
 
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic