aspose file tools*
The moose likes Java in General and the fly likes Regarding factory design pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regarding factory design pattern" Watch "Regarding factory design pattern" New topic
Author

Regarding factory design pattern

saxena neera
Greenhorn

Joined: Apr 21, 2012
Posts: 10
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

Joined: Mar 08, 2011
Posts: 188

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

Joined: Mar 08, 2011
Posts: 188

hope this would help
http://www.oodesign.com/factory-pattern.html
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1510
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8237
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
kumar naresh
Ranch Hand

Joined: Mar 12, 2011
Posts: 120
    
    1
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().


OCMJEA 5 |
http://sceacertification.blogspot.in/2011_12_01_archive.html
Viktor Pergjoka
Greenhorn

Joined: Aug 30, 2010
Posts: 20
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 ......
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding factory design pattern