• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating Objects using Factory Methods

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

What is the significance or reasons for using Factory Methods for creating Objects
For example
Or


rather than directly using tradational new operator for creating Objects .

Please share your ideas . Thanks in advance .
 
Ranch Hand
Posts: 180
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One straight advantage could be.

(1) As an API developer i dont want my clients to create the object.
Creation of object might require some additional things to do which as an API
developer i would do, would not like to give client the hassle of it.

(2) Helps me to maintain the number of instances being created.
I may want to have it as a Singleton. I do it.

Basically it gives me the security for my class and ables me to handle the lifecycle of it.

cheers,
Saurav
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Creation of object might require some additional things to do which as an API
developer i would do, would not like to give client the hassle of it.



Thanks for the above line .
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, you preserve the possibility to change your implementation later without having the client need to worry about it.

For example, you could easily return a different implementation and nobody will notice or have to change code on the client side. (This is given you work with interfaces).
 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can somebody post me a good example illustrating these mentioned concepts???
Reason for me to ask is that I am still unable to get the complete understanding of the solution provided by Saurav and Sebastian.

Regards,
Sriram
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok.

Imagine an interface to store data.


Now you write an implementation --> FileDataPersistence, and a factory method in a factory class.



You use it in your code, in 5000 different places.

Now you realize that it's so much better to use a database rather than Files, so you create a new implementation --> DatabaseDataPersistence

Instead of changing 5000 new FileDataPersistence() calls, you do the following


Voila. I know it's amazing :-)
 
Sriram Sharma
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simply Amazing!!! :-)
Very Simple and Very Clear explanation out there.
Thanks a lot Sebastian.
 
You totally ruined the moon. You're gonna hafta pay for that you know. This tiny ad agrees:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic