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
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.
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.
Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
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().