The Singleton Pattern

The singleton pattern is a software design pattern.

You can also call it the Highlander Principle: There can only be one!

But don't expect epic fights with swords and beheadings, that's only for the movie(s).

🧵👇🏻
1⃣ What is it?

As stated before, the singleton pattern is a software design pattern.

It restricts the instantiation of a class or a certain object to a single instance.

The pattern and its name are actually derived from the mathematical concept of a singleton.
In other words:

If you have a class A and make it a singleton, there can only be exactly none or exactly one instance per application.

Why none?

Because sometimes those singletons are also instantiated lazily to save resources, in case they are never needed!

👇🏻
2⃣ What is it good for?

Singletons are often used to coordinate actions across a system or to control resource access within one service.

You find singletons in state objects, facades or factories, which all often need nothing more than only one instance.
But singletons are not free of critique, as they implicitly create global state, which is often not the most desired thing to do.

Nevertheless, if you want to restrict the access to some resources, e.g., a singleton could be a great idea!
A specific database connection pool could be a great use case for a singleton, e.g.

Especially for many relational databases, connections can be pretty costly.

Opening a connection every time you want to fire a statement can potentially add a lot of overhead.
By maintaining a connection pool, and only one of it, you can ensure that everyone grabbing a connection uses the same pool.

And as creating multiple connections also costs time and resources, you only want to do it once and reuse them.

👇🏻
3⃣ Implementing a singleton in JavaScript

You can implement your singleton like you would implement any other class or object.

This example uses a class and only some basic, modern JS syntax. class ConnectionPool {   constructor(connectionProperties) {
By leveraging JS modules, you can restrict the visibility by controlling what exactly is exported.

In this case, only the function to retrieve a connection pool is exported, and thus the only interface to this module.

The constructor of the class is invisible, // somewhere else within the file  let instance = undefined;
... so that no one can instantiate another instance.

Another alternative would be to use an IIFE (immediately invoked function expression), which returns the one and only instance of an object.

This would also prevent anyone to invoke the logic once more and create another one! const instance = (function () {   // initialization logic...

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Oliver Jumpertz

Oliver Jumpertz Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @oliverjumpertz

27 Nov
DI - Dependency Injection

Dependency injection is a technique in which an object receives other objects it depends on.

It is actually one form of the broader technique inversion of control.

🧵👇🏻
1⃣ What is it?

Dependency injection is a technique where every other object (service) an object (client) depends on is injected from the outside.

So instead of simply instantiating everything on its own, a class relies on it being passed in from the outside.
You can also view it this way:

Imagine your favorite RPG character could only wear the equipment they started with. No way to change the equipment or the look of your character's equipment from the outside.

Would be boring and inflexible, wouldn't it? Image
Read 13 tweets
25 Nov
The Proxy pattern

The proxy pattern is a software design pattern.

It is basically, in its simplest form, an interface to something else.

But let's take a look at what it is and how you could use it!

🧵👇🏻
1⃣ What is it?

A Proxy is a wrapper (object), that wraps another object.

Imagine it like a container that looks exactly like the object it wraps, behaves nearly the same, but may add some additional functionality.

You can imagine it, visually, like shown below.
If you need another example:

Imagine a wetsuit. It's a thin layer above your body, keeps the shape of your body, but adds the ability to keep you dry inside!

👇🏻
Read 10 tweets
24 Nov
The Iterator Pattern

The iterator pattern is a design pattern which can be used to decouple algorithms from containers.

Let's take a look into what it actually is, how to apply it, and which problems it solves!

🧵👇
1⃣ What problems does it solve?

✅ The elements of an aggregate object should be accessed and traversed without exposing its representation (underlying data structure)

✅ New traversal operations should be defined for an aggregate object without changing its interface
Those two points may sound a little complicated, but you can view it this way:

If you define access and traversal operations within an object's interface (its methods viewable and usable from the outside), it becomes inflexible.
Read 14 tweets
22 Nov
Although I am not a data scientist by any means, I was recently asked what knowledge of math someone would need to be able to efficiently get into the field.

Well, I researched a little and came up with the following course track, starting with the basics.

🧵👇
Calculus 1 will cover a lot of the basics, relevant especially to optimization problems which are quite common in data science.

khanacademy.org/math/calculus-1
Calculus 2 will go even deeper and cover topics such as integration techniques and differential equations.

Especially integrals are pretty important for probability distributions and hypothesis testing!

khanacademy.org/math/calculus-2
Read 7 tweets
21 Nov
DIP - The Dependency Inversion Principle

The Dependency Inversion Principle is a part of SOLID, a mnemonic acronym which bundles a total of 5 design principles.

It is often associated with clean code.

But what exactly is it, is it important to you, should you even care?

🧵👇
1⃣ What does it state?

It states:

Modules that encapsulate high-level policy should not depend upon modules that implement details. Rather, both kinds of modules should depend upon abstractions.

This may sound a little complicated, but you can break it up, as follows:
1. High-level modules should not depend on low-level modules. Both should depend on abstractions (e.g., interfaces).

2. Abstractions should not depend on details. Details (concrete implementations) should depend on abstractions.
Read 17 tweets
20 Nov
ISP - The Interface Segregation Principle

The Interface Segregation Principle is part of SOLID, a mnemonic acronym which bundles a total of 5 design principles.

It is often associated with clean code.

But what exactly is it, is it important to you, should you even care?

🧵👇
1⃣ What does it state?

The ISP sets a pretty simple statement:

"No client should be forced to depend on methods it does not use."

ISP applied to the extreme will result in single method interfaces, also called role interfaces.
You can take a look at the following example to get an idea of how the ISP can be violated in one way (this time it's in TypeScript): interface ReportService {  ...
Read 14 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!