Prototype Design Pattern
Creational: Prototype Pattern
Could you please explain the prototype design pattern?
View Answer:
Interview Response: We can use the Prototype Pattern to create new objects. Rather than returning uninitialized objects, it returns objects with values copied from a prototype - or example - object. The Properties pattern is another name for the Prototype pattern.
Technical Response: We commonly refer to an object you can clone as a prototype. The Prototype Pattern creates new objects, but instead of producing uninitialized objects, it creates objects with values copied from a prototype - or example - object. The Prototype pattern is also known as the Properties pattern.
We can use the prototype pattern to create new objects based on its blueprint by cloning an existing object. The prototype pattern based on prototypal inheritance can use JavaScript's native prototyping capabilities.
We can use the prototype pattern to create new objects based on its blueprint by cloning an existing object. The prototype pattern based on prototypal inheritance can use JavaScript's native prototyping capabilities.
Diagram:


The objects participating in this pattern are:
Client -- In example code: the run() function
- creates a new object by asking a prototype to clone itself
Prototype -- In example code: CustomerPrototype
- creates an interfaces to clone itself
Clones -- In example code: Customer
- the cloned objects that are being created
Code Example:
const myCar = {
name: 'Ford Escort',
drive() {
console.log("Weeee. I'm driving!");
},
panic() {
console.log('Wait. How do you stop this thing?');
},
};
// Use Object.create to instantiate a new car
const yourCar = Object.create(myCar);
// Now we can see that one is a prototype of the other
console.log(yourCar.name);
const yourCarProto = Object.getPrototypeOf(yourCar);
console.log(yourCarProto === myCar); // true
/*
output:
Ford Escort
true
*/
The Prototype pattern belongs to which design family?
View Answer:
Interview Response: The prototype pattern is a type of creational design pattern.
What is an example of a good use case for the prototype pattern?
View Answer:
Interview Response: You can use the Prototype pattern to help initialize business objects with values that match the database's default values. The prototype object contains the default values that you can copy into a newly created business object.
Classical languages rarely use the Prototype pattern, but JavaScript is a prototypal language that uses this pattern to construct new objects and their prototypes.
We should use the Prototype pattern when your code shouldn't depend on the concrete classes of objects you need to copy.
Classical languages rarely use the Prototype pattern, but JavaScript is a prototypal language that uses this pattern to construct new objects and their prototypes.
We should use the Prototype pattern when your code shouldn't depend on the concrete classes of objects you need to copy.
What are some of the advantages of employing the Prototype pattern?
View Answer:
Interview Response: Benefits of the Prototype Pattern.
- We can clone an object without being bound to its concrete classes.
- You can avoid repeating the initialization code by cloning pre-built prototypes.
- It is easier to produce complex objects.
- When dealing with structural presets for complex objects, we produce an alternative to inheritance.
What are some of the disadvantages of employing the Prototype pattern?
View Answer:
Interview Response: Cloning complex objects with circular references might be tricky.