# Prototype

## Intent

* It is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects.
* This pattern is used to avoid subclasses of an object creator in the client application, like the factory method pattern does and to avoid the inherent cost of creating a new object in the standard way (e.g., using the 'new' keyword) when it is prohibitively expensive for a given application.

## Problems

* How can objects be created so that which objects to create can be specified at run-time?
* How can dynamically loaded classes be instantiated?

## Solutions

* Define a Prototype object that returns a copy of itself.
* Create new objects by copying a Prototype object.

## Advantages

✅ Prototype pattern also lets us create copies of objects without depending on the concrete classes.\
✅ Can get rid of repeated initialization code in favor of cloning pre-built prototypes.\


## Disadvantages

❌ It's difficult to clone classes that have circular references, [Interesting read](https://stackoverflow.com/questions/26191782/deep-copying-java-objects-with-circular-references)

***

#### Note

💡 Serialization-deserialization can be used to clone an object. 💡 Copy constructor is another way of cloning.
