Challenge: Solution Review

This lesson will explain the solution to the problem from the last coding challenge.

We'll cover the following

Solution #

Press + to interact
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 first line defines the variable configure which stores the reference to the first instance of ConfigureVals. This variable is only initialized once.

The constructor of the class accepts the object parameter initvalues. It defines and initializes the values xpoint, ypoint, and shape from lines 4-6.

this.xpoint = initvalues.xpoint || 0;
this.ypoint = initvalues.ypoint || 0;
this.shape = initvalues.shape || null;

As mentioned in the challenge, if no value of either xpoint, ypoint, or shape is given, the values are set to 0, 0, null.

The class contains the getConfiguration method, which accepts the object parameter initvalues. On line 9, the if condition checks whether an instance for the class already exists.

if (!configure)

If it does not, a new instance is created (line 10).

configure = new ConfigureVals(initvalues)

If an instance already exists, it is returned (line 12); hence, no new instance is created.

return configure;

On line 16, a new instance is created.

var configureObj1 = ConfigureVals.getConfiguration({ "xpoint": 8, "ypoint" : 9, "shape" : "rectangle" });

Since this is the first instance it is created, and you see the following output:

ConfigureVals { xpoint: 8, ypoint: 9, shape: 'rectangle' }

In line 18, the getConfiguration method is called again.

var configureObj2 = ConfigureVals.getConfiguration({ "xpoint": 2, "ypoint": 4, "shape" : "circle" });

However, since an instance already exists, the first instance is returned. Hence, you see the following output:

ConfigureVals { xpoint: 8, ypoint: 9, shape: 'rectangle' }

So both configureObj1 and configureObj2 are the same.

Press + to interact
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" });
var configureObj2 = ConfigureVals.getConfiguration({ "xpoint": 2, "ypoint": 4, "shape" : "circle" });
console.log(configureObj2 == configureObj1);

Bonus question #

Implement the same solution but using the ES5 version instead.


Let’s discuss the builder pattern in the next lesson.