Andy
Andy

Reputation: 2603

Factory Vs Prototype - What to use when?

Both Factory & Prototype help in creating objects. When designing a new application:

  1. When would you use Factory Pattern over Prototype Pattern?
  2. When would you use Prototype Pattern over Factory Pattern?

I am quite confused which one to use where.

I know it can be very much problem specific but is there any general guideline?

Thoughts?

Upvotes: 14

Views: 13510

Answers (6)

Rishi Prakash
Rishi Prakash

Reputation: 1779

Factory Design Pattern is used when you want to have a logic which can provide you/client ready to use objects of "required" type.

  • There could be more than one types of objects(multiple derived classes from one base class)
  • Object returned by create() method of Factory class is of derived class.
  • a new object is created from scratch each time client request.

On the contrary, In Prototype

  • Objects are created from scratch only once, and are then stored in registry/cache/Map for further use.

  • Objects are created and returned by using clone() method.

  • Objects returned are of one type.

    Summary - To save memory & I/O required to create an object from scratch, use prototype over Factory.

Upvotes: 0

Emanuel Moecklin
Emanuel Moecklin

Reputation: 28856

The factory patterns create objects based on a well-defined class hierarchy. The caller can pass in arguments, and the factories use them to determine which objects to create. The prototype pattern adds an extra layer of abstraction on top of the object creation process. Using the prototype pattern, objects are created (or cloned) based on runtime objects not just based on a class hierarchy.

  • Factory patterns: class hierarchy + arguments + program logic -> instantiate objects
  • Prototype pattern: class hierarchy + arguments + program logic -> instantiate prototype + arguments + program logic -> create/clone objects

An abstract factory can use prototypes under the hood to increase its flexibility in providing instances of different classes.

A more detailed explanation of the prototype pattern can be found here: https://github.com/1gravity/design_patterns/tree/main/src/main/kotlin/com/onegravity/designpatterns/prototype

Upvotes: 0

themanwhosoldtheworld
themanwhosoldtheworld

Reputation: 570

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.

Upvotes: 9

Anupam Alok
Anupam Alok

Reputation: 433

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.

Upvotes: 5

Anders Johansen
Anders Johansen

Reputation: 10455

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).

Upvotes: 5

Pierre Arlaud
Pierre Arlaud

Reputation: 4133

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).

Upvotes: 20

Related Questions