• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding factory design pattern

 
saxena neera
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks ,
I was developing the below code for factory design pattern..I was doing R&D on factory pattern I have developed the below code , please advise is it correct as per the functionality of the factory pattern..



But please suggest that what I was thinking that I can improve my code by




but for bringing this change how i would change my main java file please advise. since I have 20 java classes if added then I would not make the use of above style determining type , I wan the thing to be dynamic ,please advise what changes I need to do in my main java class
 
debraj mallick
Ranch Hand
Posts: 188
Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi neera,

we use factory design pattern when we do not know which class of object we should create, so if you know the className then no need to use factory pattern.
any suggestion or comments would be great.
 
debraj mallick
Ranch Hand
Posts: 188
Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hope this would help
http://www.oodesign.com/factory-pattern.html
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi debraj mallick,

I'm not a design pattern expert, but I think the description you've given is of abstract factory pattern (and not factory pattern).

In factory pattern, we know the name of the class, and its just that we are simply interested in 'an' object of that class (which most probably is identical to other objects - that's why it is called 'factory').

In abstract factory pattern, firstly, based on some condition, the factory of factories return us an instance of factory itself. Further we invoke getInstance on that factory.

Correct me if I'm wrong.
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:In factory pattern, we know the name of the class, and its just that we are simply interested in 'an' object of that class (which most probably is identical to other objects - that's why it is called 'factory').

Correct me if I'm wrong.

Actually, I suspect you are. The Factory pattern is used to defer the decision on what class gets created to subclasses by overriding a factory method, usually defined in an interface. AbstractFactory, in the words of Wikipedia: "provides a way to encapsulate a group of individual factories that have a common theme".

What neera has written is a simple dispatcher and, as such, I'm not sure whether it qualifies for any pattern.

Winston
 
kumar naresh
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me try to put the example in proper manner may be not in accordance to factory pattern.

If Animal is considered as an interface and Dog and Cat implements the interface, by implementing the method makeSound() in both the classes and in main method if an object instance is created like Animal a1 = new Dog(); and invoke the method makeSound().
 
Viktor Pergjoka
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also think if you really need The Factory Design Pattern, because you could use frameworks which support dependency injection like Google Guice, Spring ......
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic