Challenge: Singleton Pattern
In this challenge, you have to implement the singleton pattern to solve the given problem.
We'll cover the following
Problem statement
In this challenge, you have to implement a configuration that uses the singleton pattern. You are given a class ConfigureVals
. Define it as follows:
-
It should have a
constructor
that defines the propertiesxpoint
,ypoint
, andshape
. -
The
constructor
should initializexpoint
,ypoint
, andshape
to0
,0
, andnull
if the values for these properties are not passed to theconstructor
. -
Only a single instance of the class can be made by defining the function
getConfiguration
.
Input
The getConfiguration
method invoked
Output
The instance created after the getConfiguration
function is invoked
Sample input
getConfiguration({ xpoint: 8, ypoint : 9, shape : rectangle }); //first call
getConfiguration({ xpoint : 2, ypoint : 4, shape : circle }); //second call
Sample output
ConfigureVals { xpoint: 8, ypoint: 9, shape: 'rectangle' }
ConfigureVals { xpoint: 8, ypoint: 9, shape: 'rectangle' }
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.