Factory Vs Prototype - What to use when?

Well what i think is,

In factory Method we return the new Instance of type what we are interested in where as in prototype The related subclass return the instance of itself with clone method

Being more Specific In Factory Method creation is carried away through inheritance where as in Prototype creation through delegation i.e Polymorphism.

In my Views,

Whenever you think that you need a duplicate state of a class or the clone of object with a Pre-set State you need to use prototype.


I am going to assume you're talking about the Abstract Factory design pattern (which shouldn't be confused with the Factory Method, which is another creational design pattern).

The difference between the two is not too obvious, for they can overlap and be used in a complementary way. Since the prototype creates a clone of itself, including all its properties, it is often created by an abstract factory just once, and then cloned for every necessary object (that will not require having its fields filled again).

Prototype thus avoids unnecessary "new" calls, for the objects are cloned and not created. In most modern OOP languages however, I wouldn't say it's such a big deal. My two cents : if you don't really see the difference, just keep on using the one you're used to implementing (that is, probably the abstract factory).


As i see it, although both are creational patterns, factory and prototype patterns are used in different contexts.

Factory pattern is used to introduce loose coupling between objects as the factory will take care of all the instantiation logic hiding it from the clients.

Prototype pattern on the other hand is used when the cost of creating an object is large and it is ok to copy an existing instance than creating a new instance.


Prototype is best thought of as a way to optimize Factory (copy instead of create) or to perform dependency injection (configure factory for a specific implementation/configuration).