What type of design pattern does this belong to?

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?

Answer №1

The singleton design pattern enables access to a single instance from any part of the program without requiring a reference to be passed around.

However, it is frequently viewed as an anti-pattern due to its introduction of global state and negative impact on code testability.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Newbie in Distress: Help Required Due to Server Error

Currently working on the contacts page for a website and I need the div element to disappear once an email is successfully sent. In order to hide a Div element using Javascript, I created the following function: function hideDiv(){ document.getElement ...

The no-unused-expressions rule is triggered when an assignment or function call is expected

I'm just starting out in full stack development and I'm experimenting with coding to improve my understanding of frontend using React JS and Material UI. While working on this component, I encountered an error in the console at line 75 (this.prop ...

Tips for running code after a function finishes executing

I've developed a code that utilizes jQuery ajax to fetch a value and set it as a data attribute of an element within a function. Once the function is called, I save the data value in a variable. The issue arises when I want to ensure that no code ru ...

A program that removes rows while also decreasing the overall sum

I have implemented a form that allows users to add labor and its price, while also displaying the assigned labor on the same page underneath. My objective is to have functionality where if a user clicks on delete, it will remove the corresponding table ro ...

Code remaining stable post-execution of promise

I'm facing a problem with my Node.js application where I'm using promise-mysql and bluebird packages to make calls to a MySQL database. Despite following tutorials and successfully querying the database, I keep encountering a timeout error. The p ...

JavaScript: Extracting filename from an URL - Does JavaScript continue its operation in the background?

Currently, I have an image upload form that provides the option to either select a file or paste a URL. After selecting one of these options, the image is previewed and the filename is displayed below. However, there seems to be an issue when pasting ...

"Encountering a 'Cannot GET' error message while utilizing Rest API in Node.js

Currently, I am developing a project using nodejs along with the expressjs framework. My focus right now is on setting up and running a "Rest Api," but I seem to be encountering an error message that reads: Cannot GET /published Let me share my routes fil ...

Retrieve the link of a nearby element

My goal is to create a userscript that has the following functionalities: Add a checkbox next to each hyperlink When the checkbox is clicked, change the state of the corresponding hyperlink to "visited" by changing its color from blue to violet. However ...

Trouble with FilterBy Feature in Bootstrap-Table

I am attempting to use filtering functionality with a table that is populated via JSON, utilizing bootstrap-table by wenzhixin. HTML snippet: <button class="btn btn-primary" id="filterBtn">Filter</button> <div class="container"> &l ...

What is the process through which Generator.next() works with its parameter?

According to the documentation, it mentions that "You can also provide a parameter to the next method to send a value to the generator." But where exactly does it send this value to? Let's consider these 3 different generator functions: function* fi ...

What is the best way to transfer an object from the view to the controller in AngularJS and ASP.net MVC

How to pass an object with a specific amount of data from the View to the Controller using ASP.net MVC and AngularJS VIEW var Person = {}; Person.IdPerson = 69425; Person.Year = new Date().getFullYear(); $http.post('/API/Update_Person', { para ...

Exploring the caching capabilities of NextJS and Vercel for optimizing simple fetch

I have successfully deployed a NextJS (13.2.4) app to Vercel after testing it locally with great results. The issue I am facing is related to a simple fetch request from a component ('use client'). This request is directed towards a route within ...

The loading time for the Ajax request is unreasonably slow

I am currently managing a website dedicated to League of Legends. My main task involves requesting statistics from the Riot Games API based on a player's name, which returns the information in JSON format. However, there is a significant delay in load ...

Custom AngularJS Directive for managing an array of Scope data

I have an array where each item contains data for a specific directive. This array is initialized within a controller. The Model $scope.myDirectiveData = [ { title: "First title", content: "First content" }, { title: "Second title ...

Enhancing jQuery Rating Plugin

Currently, I am working on customizing the jQuery Bar Rating System Plugin. You can view an example of the plugin by visiting this link: . The rating system on my end will resemble Example D. Rather than having the plugin based on user input, my goal is to ...

What is the most effective way to use Javascript to update an image depending on the selected value in a dropdown

My JavaScript skills are limited, but I have successfully created a form with 4 drop-down selection boxes on the left side for users to choose from (State, Car, Year, Condition). After making their selections, users can click a 'calculate' butto ...

Run a series of Mocha unit tests based on the outcomes of previous test results

Currently, I am in the process of writing unit tests for a NodeJS application that I am developing. In relation to some unit-testing logic, I have a question. Imagine if the application first creates a "Group" for users, followed by creating individual Us ...

What is the best way to modify the KeyName in an object?

Having difficulty parsing an Object by changing keynames due to the error message "Element implicitly has an 'any' type because expression of type 'keyof SignInStore' can't be used to index type '{}'". interface SignInSto ...

The Bootstrap modal seems unable to bring the button back

Despite my code functioning properly, the button is not going backward after being clicked. Check out my output here <a href="#myModal" role="button" class="btn btn-large btn-primary" data-toggle="modal">Launch Demo Modal</a> Here is the HTML ...

The function JSON.parse(data) may result in an undefined value being returned

data = { "users": [ [{ "value": "01", "text": "ABC XYZ" }], [{ "value": "02", "text": "XYZ ABC" }] ] } var jsonData = JSON.parse(data); for (var i = 0; i < jsonData.users.length; i++) { var userlist = json ...