Today, as I was exploring a TypeScript repository, I noticed that all the classes were structured in a particular way:
export class ServiceClass {
private static instance: ServiceClass;
private constructor() {}
public static Instance(): ServiceClass {
if (!ServiceClass.instance) {
ServiceClass.instance = new ServiceClass();
}
return ServiceClass.instance;
}
// public static ServiceMethod1
// public static ServiceMethod2
// public static ServiceMethod3
}
The methods of the class were called using
new ServiceClass.Instance().ServiceMethod1()
.
I wondered about the purpose of having the Instance()
method. If only the service methods were included in the class and accessed like this:
const obj = new ServiceClass();
obj.ServiceMethod1();
Could this be a specific design pattern at play here?