Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Singleton {
- private static instance: Singleton;
- private constructor() {
- // Private constructor to prevent instantiation
- }
- static getInstance(): Singleton {
- if (!Singleton.instance) {
- Singleton.instance = new Singleton();
- }
- return Singleton.instance;
- }
- public someMethod(): void {
- console.log("Some method called");
- }
- }
- // Usage
- const singletonInstance1 = Singleton.getInstance();
- const singletonInstance2 = Singleton.getInstance();
- console.log(singletonInstance1 === singletonInstance2); // Output: true, there is only one instance
- singletonInstance1.someMethod(); // Output: Some method called
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement