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
. -
Make sure that 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' }
Challenge
Take a close look, and design a step-by-step solution before jumping on to the implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the solution provided. Good Luck!
class ConfigureVals{//write your code here}
Let’s look at the solution in the next lesson.