Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

273
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda