singleton-wrapper
v1.2.2
Published
reusable javascript design pattern wrappers
Downloads
15
Readme
singleton-wrapper
To enable JS programmers to implement the singleton pattern in their application at ease.
Suppose there is a class written that is used in multiple places and has an object instantiated in multiple places using the constructor.
If you want to have only one instance of the class in the entire application with minimal changes, then this package helps you without modifying class and the use of a constructor to instantiate the object.
steps to implement
install using command
npm i singleton-wrapper
once installation completed just import the Singleton class and export default the wrapper class as below.
import Singleton from 'singleton-wrapper';
export default class {
constructor() {
return new Singleton(MyClass);
}
}
class MyClass {
constuctor(){
}
}
parameterized constructor
If you have parameterized constructor, then pass the parameter as an collected object, as below:
import Singleton from 'singleton-wrapper';
export default class {
constructor(arg1, arg2, ...) {
return new Singleton(MyClass, [arg1, arg2, ...]);
}
}
class MyClass {
constuctor(arg1, ag2, ....){
}
}
It makes sure MyClass will have only one object in the application.