• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

advantages of factory methods

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what are the advantages of factory methods ?
and when should i need to use them?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find a lot of information by searching with (for example) Google. Within 10 seconds you'll find this if you search for "factory method":

The Factory Method (Creational) Design Pattern
JavaWorld: Factory methods
Java Practices: Factory methods
Pattern Summaries: Factory Method
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am going to assume you know how to google and that you are rather looking for a point of view that you can interact with. Moreover, every question on this forum could be preceded by "you can get information from google"; so it is pointless to suggest that.

Factory methods insulate your application from custom code. Creating custom versions of an application is therefore simplified.

Assume you have several customers and they all have specialized needs. Sprinkling your code with conditional statements everywhere there was a customer variance would create a maintenance nightmare. Instead, you can capture variances in a class or set of classes with a standard interface. Factory methods are then used to select the appropariate sub-type at runtime. The rest of your application is unaware that it's running a specialized version of the application. The same application can support an infinite variety of implementations withou change.

Note that you still have the conditional statement that decides which class to instantiate, however, it's all neatly packaged inside a factory method that creates
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for ur valuable site links (resources)
cinux
 
Kai Witte
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

I recently wrote a Design Principles article where I also state my opinion regarding the applicability of factory methods (section "Encapsulate instance control").

Kai
[ February 06, 2006: Message edited by: Kai Witte ]
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i 've seen all the links.
but i ain't getting the actual concept ?? :roll:
can anyone provide me an example program for the implementation of factory methods
thanx in advance
cinux
 
Craig Tyler
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take the Calendar class as an example. It uses a factory method to give you a calendar instance based on what your default locale would use. So, while you always use a Calendar reference variable, the actual class of the object will vary.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Craig Tyler:
Take the Calendar class as an example. It uses a factory method to give you a calendar instance based on what your default locale would use. So, while you always use a Calendar reference variable, the actual class of the object will vary.


Notice that this is *not* the Factory Method design pattern. The design pattern involves a polymorphic method call inside a class hierarchy to create an object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic