How to implement Singleton in Node.js
singleton
3
nodejs
133
Male avatar

loveprogramming viết ngày 04/04/2021

How to implement Singleton in Node.js

https://grokonez.com/node-js/how-to-implement-singleton-in-node-js-example

Singleton is object that can have only a single, unique instance, with a single point of access. Node.js module system provides simple way to implement Singleton using module.exports. Module will be cached when it is accessed using require() statement. So our module is merely a cached instance although it behaves like a Singleton.

In this tutorial, we're gonna look at ways to implement this kind of Singleton in Node.js:

  • Singleton Object
  • Singleton Class

Node.js Singleton Object example

We will create a Bank object, it has 3 methods:

  • deposit() increases cash.
  • withdraw() decreases cash.
  • total() returns cash.

Bank.js


let cash = 0;

const Bank = {
    deposit(amount) {
        cash += amount;
        return cash;
    },
    withdraw(amount) {
        if (amount 

Bank object behaves like a Singleton because we will use module.exports and require() statement (not use new keyword).

app.js


const fund = require('./Bank');
const atm1 = require('./Bank');
const atm2 = require('./Bank');

fund.deposit(10000)

atm1.deposit(20)
atm2.withdraw(120)

console.log(`total-atm1: ${atm1.total()}`)
console.log(`total-atm2: ${atm2.total()}`)

fund.deposit(2000)
console.log(`total-fund: ${fund.total()}`)

As you can see, we create 3 Banks (fund, atm1, atm2), every Bank object can deposit or withdraw money. But all of the action work with only one the cash insides Bank.

Here is the result (run with node app.js command):


total-atm1: 9900
total-atm2: 9900
total-fund after funding 2000: 11900

More at: https://grokonez.com/node-js/how-to-implement-singleton-in-node-js-example

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

451 bài viết.
79 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 6 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 5 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 5 tháng trước
1 0
Bài viết liên quan
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 5 tháng trước
1 0
Male avatar
0 0
https://grokonez.com/kotlin/kotlinsingletonobjectdeclaration Kotlin Singleton with Object Declaration In object oriented programming, there is a ...
loveprogramming viết 7 ngày trước
0 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
451 bài viết.
79 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!