Search⌘ K
AI Features

Challenge: Solution Review

Explore the singleton pattern implementation that controls object creation in JavaScript. Understand how to restrict instantiation to a single instance while managing default and custom values effectively. This lesson helps you grasp design patterns critical for optimizing object construction in coding interviews.

We'll cover the following...

Solution

Node.js
let configure = null;
class ConfigureVals{
constructor(initvalues){
this.xpoint = initvalues.xpoint || 0;
this.ypoint = initvalues.ypoint || 0;
this.shape = initvalues.shape || null;
}
static getConfiguration(initvalues){
if (!configure) {
configure = new ConfigureVals(initvalues)
}
return configure;
}
}
var configureObj1 = ConfigureVals.getConfiguration({ "xpoint": 8, "ypoint" : 9, "shape" : "rectangle" });
console.log(configureObj1);
var configureObj2 = ConfigureVals.getConfiguration({ "xpoint": 2, "ypoint": 4, "shape" : "circle" });
console.log(configureObj2);

Explanation

In the solution, the ...