Definition:

Specify the types of objects to create with prototype instances, and create new objects by copying these prototypes.

Usage Scenarios:

  • Class initialization needs to absorb a lot of resources, such as data, hardware resources, and so on.
  • If generating an object from New requires very tedious data preparation or access rights, you can use prototype mode;
  • When an object needs to be made available to other objects, and each caller may need to modify its value, you can consider copying multiple objects for use by callers using the prototype pattern, known as protective copying.

UML class diagram:

Advantages:

The prototype pattern is a copy of the binary stream in memory and performs much better than simply new an object, especially when creating a large number of objects in a loop.

Disadvantages:

This is both a strength and a weakness. The constructor is not executed when it is copied directly in memory, and this potential problem should be noted during actual development. The advantage is to reduce the constraints, the disadvantage is also to reduce the constraints, we need to consider in the practical application.