1

Recently, while learning about Design Patterns, I learnt that the Prototype Pattern is very useful and performance efficient in scenarios where huge number of Object creation is needed.

Prototype Pattern also minimizes the expense of too many object creations by making use of Cloneable interface or Copy Constructor in Prototype Pattern.

But, I would like to know how does cloning or copying an object more efficient than creating a new object. A JVM level explanation would be great.

Is this the only reason Prototype Pattern is made used?

vikash singh
  • 1,479
  • 1
  • 19
  • 29
prashanth
  • 69
  • 7
  • [cloning is broken](https://www.artima.com/intv/bloch13.html) and should not be used. A copy constructor has to be implemented and then called using `new`, so it's nothing else than creating a new object. What makes you think the pattern creates objects more efficiently? – f1sh Oct 04 '19 at 07:26
  • Here is the course link I followed that explained that prototype is used when we want to create a large amount of objects efficiently [pluralsight_course_design_patterns](https://app.pluralsight.com/library/courses/design-patterns-java-creational/table-of-contents) – prashanth Oct 04 '19 at 09:34

3 Answers3

2

The prototype pattern isn't used for performance (although according to Ioannis' link, it has been used as a performance hack). It's used so you can create new objects from a (possibly changing) prototype.

Some method of "cloning" is needed so you don't have to care about the state of prototype. You can just call prototype.someMethodThatReturnsACopy() and the object is ready for use. You could use clone() or some other way to create that copy, even manually constructing one if you really want.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • So what are the common use of this Design Pattern? why was this created? And is there any example where it is used by a Java Library? – prashanth Oct 04 '19 at 09:50
  • 1
    It's an old pattern, part of the [GoF](https://en.wikipedia.org/wiki/Design_Patterns) book. It's unlikely that you'd see it directly implemented anywhere. However the related factory pattern is still somewhat common in (Java) code. – Kayaman Oct 04 '19 at 09:56
  • Is Factory pattern related to Prototype Pattern? – prashanth Oct 04 '19 at 10:33
  • Yes, that's why I wrote "the related factory pattern". – Kayaman Oct 04 '19 at 10:33
2

It is interesting that you recently learned that Prototype Pattern is performant as that design pattern is outdated. You can check this stackoverflow question which is almost 10 years old

Ioannis Barakos
  • 1,319
  • 1
  • 11
  • 16
0

Previous answers are correct in that the Prototype pattern is not a useful performance tool in Java.

The GoF book states on page 121,

Prototype is particularly useful with static languages like C++, where classes are not objects, and little or no type information is available at run-time. It's less important in languages like Smalltalk or Objective C that provide what amounts to a prototype (i.e., a class object) for creating instances of each class. This pattern is built into prototype-based languages like Self, in which all object creation happens by cloning a prototype.

A more modern prototype-based language is JavaScript. Some pros and cons are discussed in prototype based vs. class based inheritance.

jaco0646
  • 15,303
  • 7
  • 59
  • 83