Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

281
Views
What is the purpose of a `getInstance()` method?

I am looking at a user service, my understanding is it's similar to a user service in Nest, but not really.

In it I see the following:

export class UsersService {
  private usersDao: UsersDao

  constructor() {
     this.usersDao = UsersDao.getInstance();
  }
}

static getInstance(): UsersService {
  if (!UsersService.instance) {
    UsersService.instance = new UsersService();
  }
  return UsersService.instance;
}

What is that getInstance() doing exactly? And why not just:

export class UsersService {

  constructor(private usersDao: UsersDao) {}
}

What is the goal of getInstance()?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Usually this is part of the singleton pattern. Basically one class that, once instantiated, any subsequent classes will refer to that instance, rather than creating a fresh instance each time.

https://en.wikipedia.org/wiki/Singleton_pattern

Its useful for a class where something complex needs to happen when it is first constructed, but all following calls just need access to the properties.

I'd also like to mention that you can (in JavaScript specifically) export an instance, and all modules that import the module will have access to the same instance.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!