<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ketan Jakhar</title>
    <description>The latest articles on Forem by Ketan Jakhar (@codexstoney).</description>
    <link>https://forem.com/codexstoney</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F587846%2Fbf19b0f3-4795-4f30-8c27-882895d90a35.jpg</url>
      <title>Forem: Ketan Jakhar</title>
      <link>https://forem.com/codexstoney</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/codexstoney"/>
    <language>en</language>
    <item>
      <title>Mastering Design Patterns in JavaScript: Part 9 — The Mediator Pattern</title>
      <dc:creator>Ketan Jakhar</dc:creator>
      <pubDate>Wed, 04 Dec 2024 16:51:01 +0000</pubDate>
      <link>https://forem.com/codexstoney/mastering-design-patterns-in-javascript-part-9-the-mediator-pattern-40gd</link>
      <guid>https://forem.com/codexstoney/mastering-design-patterns-in-javascript-part-9-the-mediator-pattern-40gd</guid>
      <description>&lt;p&gt;&lt;a href="https://javascript.plainenglish.io/mastering-design-patterns-in-javascript-part-9-the-mediator-pattern-62969be077c5?source=rss-b6d62e9eccec------2" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F35070y07d1jk04iznwhe.png" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Mediator Pattern — a design approach that centralizes communication to make your code more maintainable and less tangled.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://javascript.plainenglish.io/mastering-design-patterns-in-javascript-part-9-the-mediator-pattern-62969be077c5?source=rss-b6d62e9eccec------2" rel="noopener noreferrer"&gt;Continue reading on JavaScript in Plain English »&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>node</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Mastering Design Patterns in JavaScript: Part 8 — The Chain of Responsibility Pattern</title>
      <dc:creator>Ketan Jakhar</dc:creator>
      <pubDate>Thu, 21 Nov 2024 16:19:21 +0000</pubDate>
      <link>https://forem.com/codexstoney/mastering-design-patterns-in-javascript-part-8-the-chain-of-responsibility-pattern-2944</link>
      <guid>https://forem.com/codexstoney/mastering-design-patterns-in-javascript-part-8-the-chain-of-responsibility-pattern-2944</guid>
      <description>&lt;p&gt;&lt;a href="https://javascript.plainenglish.io/mastering-design-patterns-in-javascript-part-8-the-chain-of-responsibility-pattern-eebefbb65d58?source=rss-b6d62e9eccec------2" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk8mz0lw117l3xzchlhzr.png" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Implementing the Chain of Responsibility Pattern in JavaScript for Clean, Scalable Code — Enhance Request Handling Today&lt;/p&gt;

&lt;p&gt;&lt;a href="https://javascript.plainenglish.io/mastering-design-patterns-in-javascript-part-8-the-chain-of-responsibility-pattern-eebefbb65d58?source=rss-b6d62e9eccec------2" rel="noopener noreferrer"&gt;Continue reading on JavaScript in Plain English »&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>react</category>
      <category>node</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Mastering Design Patterns in JavaScript: Part 7 — The Command Pattern</title>
      <dc:creator>Ketan Jakhar</dc:creator>
      <pubDate>Tue, 12 Nov 2024 01:32:08 +0000</pubDate>
      <link>https://forem.com/codexstoney/mastering-design-patterns-in-javascript-part-6-the-command-pattern-4dil</link>
      <guid>https://forem.com/codexstoney/mastering-design-patterns-in-javascript-part-6-the-command-pattern-4dil</guid>
      <description>&lt;p&gt;&lt;a href="https://javascript.plainenglish.io/mastering-design-patterns-in-javascript-part-6-the-command-pattern-851b0453fdf0?source=rss-b6d62e9eccec------2" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fobs481djxad3opu1cfuc.png" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Encapsulate actions as objects in JS with the Command Pattern, enabling undo/redo, task scheduling, and flexible, decoupled operations.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://javascript.plainenglish.io/mastering-design-patterns-in-javascript-part-6-the-command-pattern-851b0453fdf0?source=rss-b6d62e9eccec------2" rel="noopener noreferrer"&gt;Continue reading on JavaScript in Plain English »&lt;/a&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>react</category>
      <category>typescript</category>
      <category>designpatterns</category>
    </item>
    <item>
      <title>Mastering Design Patterns in JavaScript: Part 6 — The Adapter Pattern</title>
      <dc:creator>Ketan Jakhar</dc:creator>
      <pubDate>Fri, 08 Nov 2024 11:27:21 +0000</pubDate>
      <link>https://forem.com/codexstoney/mastering-design-patterns-in-javascript-part-6-the-adapter-pattern-mki</link>
      <guid>https://forem.com/codexstoney/mastering-design-patterns-in-javascript-part-6-the-adapter-pattern-mki</guid>
      <description>&lt;p&gt;&lt;a href="https://javascript.plainenglish.io/mastering-design-patterns-in-javascript-part-6-the-adapter-pattern-dc899a09f7d7?source=rss-b6d62e9eccec------2" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6dhs2tivhqurnm72siuh.png" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Master the Adapter Pattern in JavaScript to seamlessly integrate incompatible interfaces. Learn through practical examples, best practices,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://javascript.plainenglish.io/mastering-design-patterns-in-javascript-part-6-the-adapter-pattern-dc899a09f7d7?source=rss-b6d62e9eccec------2" rel="noopener noreferrer"&gt;Continue reading on JavaScript in Plain English »&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>node</category>
      <category>designpatterns</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Mastering Design Patterns in JavaScript: Part 5 — The Decorator Pattern</title>
      <dc:creator>Ketan Jakhar</dc:creator>
      <pubDate>Tue, 05 Nov 2024 08:53:25 +0000</pubDate>
      <link>https://forem.com/codexstoney/mastering-design-patterns-in-javascript-part-5-the-decorator-pattern-4353</link>
      <guid>https://forem.com/codexstoney/mastering-design-patterns-in-javascript-part-5-the-decorator-pattern-4353</guid>
      <description>&lt;p&gt;&lt;a href="https://javascript.plainenglish.io/mastering-design-patterns-in-javascript-part-5-the-decorator-pattern-a728dd7418fd?source=rss-b6d62e9eccec------2" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fanu62byji23kibdmlb8s.png" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hey folks! 👋&lt;/p&gt;

&lt;p&gt;&lt;a href="https://javascript.plainenglish.io/mastering-design-patterns-in-javascript-part-5-the-decorator-pattern-a728dd7418fd?source=rss-b6d62e9eccec------2" rel="noopener noreferrer"&gt;Continue reading on JavaScript in Plain English »&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>react</category>
      <category>node</category>
    </item>
    <item>
      <title>Mastering Design Patterns in JavaScript: Part 4 — The Strategy Pattern</title>
      <dc:creator>Ketan Jakhar</dc:creator>
      <pubDate>Mon, 28 Oct 2024 12:25:05 +0000</pubDate>
      <link>https://forem.com/codexstoney/mastering-design-patterns-in-javascript-part-4-the-strategy-pattern-3f9l</link>
      <guid>https://forem.com/codexstoney/mastering-design-patterns-in-javascript-part-4-the-strategy-pattern-3f9l</guid>
      <description>&lt;p&gt;&lt;a href="https://javascript.plainenglish.io/mastering-design-patterns-in-javascript-part-4-the-strategy-pattern-9cad851cd3fe?source=rss-b6d62e9eccec------2" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnsghax2cvfphkl71nnqs.png" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Master the Strategy Pattern to simplify code, boost flexibility, and replace complex conditionals with reusable, maintainable strategies.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://javascript.plainenglish.io/mastering-design-patterns-in-javascript-part-4-the-strategy-pattern-9cad851cd3fe?source=rss-b6d62e9eccec------2" rel="noopener noreferrer"&gt;Continue reading on JavaScript in Plain English »&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>programming</category>
      <category>react</category>
    </item>
    <item>
      <title>Mastering Design Patterns in JavaScript: Part 3 — The Observer Pattern</title>
      <dc:creator>Ketan Jakhar</dc:creator>
      <pubDate>Mon, 21 Oct 2024 11:06:10 +0000</pubDate>
      <link>https://forem.com/codexstoney/mastering-design-patterns-in-javascript-part-3-the-observer-pattern-447l</link>
      <guid>https://forem.com/codexstoney/mastering-design-patterns-in-javascript-part-3-the-observer-pattern-447l</guid>
      <description>&lt;p&gt;&lt;a href="https://javascript.plainenglish.io/mastering-design-patterns-in-javascript-part-3-the-observer-pattern-06f1a08ade93?source=rss-b6d62e9eccec------2" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm1dcislth5235z6eu9b2.png" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Learn to implement the Observer Pattern in JavaScript, enhancing app scalability, modularity, and event handling for efficient…&lt;/p&gt;

&lt;p&gt;&lt;a href="https://javascript.plainenglish.io/mastering-design-patterns-in-javascript-part-3-the-observer-pattern-06f1a08ade93?source=rss-b6d62e9eccec------2" rel="noopener noreferrer"&gt;Continue reading on JavaScript in Plain English »&lt;/a&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>react</category>
      <category>designpatterns</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Mastering Design Patterns in JavaScript: Part 2 — The Factory Pattern</title>
      <dc:creator>Ketan Jakhar</dc:creator>
      <pubDate>Sat, 19 Oct 2024 11:59:25 +0000</pubDate>
      <link>https://forem.com/codexstoney/mastering-design-patterns-in-javascript-part-2-the-factory-pattern-2c0j</link>
      <guid>https://forem.com/codexstoney/mastering-design-patterns-in-javascript-part-2-the-factory-pattern-2c0j</guid>
      <description>&lt;p&gt;&lt;a href="https://javascript.plainenglish.io/mastering-design-patterns-in-javascript-part-2-the-factory-pattern-6a0c96295c01?source=rss-b6d62e9eccec------2" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy6njf7xlfp9208dz1ikl.png" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Welcome back to our deep dive into design patterns in JavaScript! If you joined us for the first part, we explored the Singleton Pattern…&lt;/p&gt;

&lt;p&gt;&lt;a href="https://javascript.plainenglish.io/mastering-design-patterns-in-javascript-part-2-the-factory-pattern-6a0c96295c01?source=rss-b6d62e9eccec------2" rel="noopener noreferrer"&gt;Continue reading on JavaScript in Plain English »&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>designpatterns</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Mastering Design Patterns in JavaScript: Part 1 — The Singleton Pattern</title>
      <dc:creator>Ketan Jakhar</dc:creator>
      <pubDate>Wed, 16 Oct 2024 12:28:33 +0000</pubDate>
      <link>https://forem.com/codexstoney/mastering-design-patterns-in-javascript-part-1-the-singleton-pattern-1nkh</link>
      <guid>https://forem.com/codexstoney/mastering-design-patterns-in-javascript-part-1-the-singleton-pattern-1nkh</guid>
      <description>&lt;h3&gt;
  
  
  Mastering Design Patterns in JavaScript: Part 1 — The Singleton Pattern
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fei0s62tgqu1muphd1nyk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fei0s62tgqu1muphd1nyk.png" width="800" height="457"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Generated using AI&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Welcome to the first part of our comprehensive series on design patterns in JavaScript! Whether you’re a newbie or an experienced developer, understanding design patterns is a game-changer for writing clean, efficient, and maintainable code. In this part, we’ll delve deep into one of the simplest yet impactful patterns: the &lt;strong&gt;Singleton Pattern&lt;/strong&gt;. 🎉&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  What Exactly is the Singleton Pattern? 🤔
&lt;/h3&gt;

&lt;p&gt;Imagine you’re building an application and need a class that should only have &lt;strong&gt;one instance&lt;/strong&gt; throughout the entire app. Perhaps it’s a configuration manager, a logging utility, or a connection to a database. You don’t want multiple copies running around confusing.&lt;/p&gt;

&lt;p&gt;That’s where the &lt;strong&gt;Singleton Pattern&lt;/strong&gt; comes into play. It’s a design approach that ensures a class has only one instance and provides a global point of access to it.&lt;/p&gt;

&lt;p&gt;When I first heard about Singletons, I thought, “Will I ever need just one instance of a class?” Then I ran into a project where multiple instances of a configuration object led to inconsistent settings across the app. Implementing a Singleton solved that problem beautifully.&lt;/p&gt;
&lt;h3&gt;
  
  
  Why Use the Singleton Pattern❓
&lt;/h3&gt;
&lt;h4&gt;
  
  
  1. Controlled Access to a Single Instance
&lt;/h4&gt;

&lt;p&gt;By ensuring only one instance exists, you prevent conflicting states and maintain data consistency across your application. This is crucial for resources like database connections, logging mechanisms, or configuration settings. 🗄️&lt;/p&gt;
&lt;h4&gt;
  
  
  2. Lazy Initialization
&lt;/h4&gt;

&lt;p&gt;Singletons are often &lt;strong&gt;lazily initialized&lt;/strong&gt;  — they’re created only when needed. This can improve performance by delaying heavy computations or resource-intensive setups until necessary. 💤&lt;/p&gt;
&lt;h4&gt;
  
  
  3. Global Access Point
&lt;/h4&gt;

&lt;p&gt;Having a global access point simplifies the code needed to access certain resources. You don’t need to pass around instances; you can simply import or require the Singleton wherever it’s needed. 🌐&lt;/p&gt;
&lt;h3&gt;
  
  
  Implementing the Singleton Pattern in JavaScript 💻
&lt;/h3&gt;

&lt;p&gt;Let’s get our hands dirty with some code!&lt;/p&gt;
&lt;h4&gt;
  
  
  Using Closures
&lt;/h4&gt;

&lt;p&gt;One way to create a Singleton in JavaScript is by using closures:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Singleton = (function () {
  let instance; // Private variable to hold the single instance

  function init() {
    // Private methods and variables
    let privateVariable = 'I am private';
    function privateMethod() {
      console.log('I am a private method');
    }

    return {
      // Public methods and variables
      publicMethod: function () {
        console.log('I am a public method');
      },
      publicVariable: 'I am public',
    };
  }

  return {
    getInstance: function () {
      if (!instance) {
        instance = init();
      }
      return instance;
    },
  };
})();

const instance1 = Singleton.getInstance();
const instance2 = Singleton.getInstance();

console.log(instance1 === instance2); // Outputs: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We use an Immediately Invoked Function Expression (IIFE) to create a private scope.&lt;/li&gt;
&lt;li&gt;The instance variable holds our Singleton instance.&lt;/li&gt;
&lt;li&gt;The getInstance method checks if an instance exists. If not, it creates one.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Using ES6 Classes🎩
&lt;/h4&gt;

&lt;p&gt;With modern JavaScript, we can implement a Singleton using classes for a cleaner approach:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Singleton {
  constructor() {
    if (Singleton.instance) {
      return Singleton.instance;
    }
    Singleton.instance = this;

    // Initialization code here
    this.data = 'Some data';
  }
}

const obj1 = new Singleton();
const obj2 = new Singleton();

console.log(obj1 === obj2); // Output: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here’s what’s happening:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We check if Singleton.instance exists in the constructor.&lt;/li&gt;
&lt;li&gt;If it does, we return that existing instance.&lt;/li&gt;
&lt;li&gt;Otherwise, we set Singleton.instance to this.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Implementing a Singleton in TypeScript
&lt;/h4&gt;

&lt;p&gt;TypeScript allows us to enhance this pattern with type safety:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Singleton {
  private static instance: Singleton;

  private constructor() {
    // Some code here
  }

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

}

const obj1 = Singleton.getInstance();
const obj2 = Singleton.getInstance();

console.log(obj1 === obj2); // Output: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key Points:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The constructor is marked as private, preventing direct instantiation.&lt;/li&gt;
&lt;li&gt;We provide a static getInstance method to control the instantiation.&lt;/li&gt;
&lt;li&gt;This ensures only one instance of the class can exist.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Pros and Cons ⚖️
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Pros👍
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Single Point of Access&lt;/strong&gt; : Makes it easy to manage shared resources.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Consistency&lt;/strong&gt; : All parts of your application use the same instance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lazy Instantiation&lt;/strong&gt; : The instance is created only when needed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Cons👎
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Global State&lt;/strong&gt; : Singletons introduce a global state into your application, which can make debugging tricky.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Testing Challenges&lt;/strong&gt; : Mocking Singletons in tests can be more complex.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tight Coupling&lt;/strong&gt; : Components become dependent on the Singleton, reducing modularity.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  When to Use Singletons ❓
&lt;/h3&gt;

&lt;p&gt;Singletons are useful in scenarios where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Resource Sharing&lt;/strong&gt; : You have a resource-intensive object that shouldn’t be duplicated (like a database connection).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Global Configuration&lt;/strong&gt; : Application settings that need to be accessed throughout the app.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Logging Services&lt;/strong&gt; : Maintaining a consistent logging mechanism.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, in one of my projects, we used a Singleton for the configuration manager. This ensured that no matter where we accessed the config, we were always getting the same settings.&lt;/p&gt;

&lt;h3&gt;
  
  
  Best Practices 🌟
&lt;/h3&gt;

&lt;p&gt;To get the most out of Singletons while avoiding pitfalls:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use Them Sparingly&lt;/strong&gt; : Overusing Singletons can lead to code that’s hard to maintain.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep It Simple&lt;/strong&gt; : Don’t overload your Singleton with too much functionality.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Be Mindful of Testing&lt;/strong&gt; : Consider how you’ll mock or stub Singletons in your tests.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Common Pitfalls to Avoid 🚫
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Hidden Dependencies
&lt;/h4&gt;

&lt;p&gt;Relying on Singletons can create hidden dependencies in your code, making it less transparent and harder to maintain.&lt;/p&gt;

&lt;h4&gt;
  
  
  Difficulty in Unit Testing
&lt;/h4&gt;

&lt;p&gt;Since Singletons introduce a global state, they can make unit tests less reliable if not handled properly.&lt;/p&gt;

&lt;h4&gt;
  
  
  Concurrency Issues
&lt;/h4&gt;

&lt;p&gt;In environments where multiple processes or threads are involved, ensuring the Singleton remains truly single can be challenging.&lt;/p&gt;

&lt;h3&gt;
  
  
  Alternatives to Singletons 🤔
&lt;/h3&gt;

&lt;p&gt;While Singletons can be useful, sometimes other patterns might be a better fit.&lt;/p&gt;

&lt;h4&gt;
  
  
  Dependency Injection🧩
&lt;/h4&gt;

&lt;p&gt;Instead of relying on global instances, you can pass dependencies explicitly where needed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class UserService {
  constructor(logger) {
    this.logger = logger;
  }

  createUser(user) {
    // Create user logic
    this.logger.log('User created');
  }
}

const logger = new Logger();
const userService = new UserService(logger);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach makes your code more modular and easier to test.&lt;/p&gt;

&lt;h3&gt;
  
  
  Real-World Use Cases 🌍
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Configuration Manager⚙️
&lt;/h4&gt;

&lt;p&gt;Having a single configuration manager ensures that all parts of your application read from the same set of configurations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ConfigManager {
  constructor() {
    if (ConfigManager.instance) {
      return ConfigManager.instance;
    }
    this.settings = {};
    ConfigManager.instance = this;
  }

  get(key) {
    return this.settings[key];
  }

  set(key, value) {
    this.settings[key] = value;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Logging Service📝
&lt;/h4&gt;

&lt;p&gt;A singleton logging service ensures all logs are written in a consistent format and stored in the same place.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Logger {
  constructor() {
    if (Logger.instance) {
      return Logger.instance;
    }
    Logger.instance = this;
  }

  log(message) {
    console.log(`[${new Date().toISOString()}] ${message}`);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion 🎯
&lt;/h3&gt;

&lt;p&gt;The Singleton Pattern is a simple yet powerful tool that, when used appropriately, can help you manage shared resources effectively. But with great power comes responsibility! Be mindful of the potential downsides, and consider whether a Singleton best fits your particular scenario.&lt;/p&gt;

&lt;p&gt;Thanks for joining me on this deep dive into the Singleton Pattern. I hope you found it helpful! In the next part of this series, we’ll tackle the &lt;strong&gt;Factory Pattern&lt;/strong&gt;. It’s a fantastic design pattern that helps with object creation, making your code more flexible and scalable.&lt;/p&gt;

&lt;p&gt;Feel free to share your thoughts or questions in the comments below. Let’s keep the conversation going! 💬&lt;/p&gt;

&lt;h3&gt;
  
  
  References 📚
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://refactoring.guru/design-patterns/singleton" rel="noopener noreferrer"&gt;Refactoring Guru — Singleton Pattern&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Singleton_pattern" rel="noopener noreferrer"&gt;Wikipedia — Single Pattern&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Peace out!✌️&lt;/p&gt;
&lt;/blockquote&gt;




</description>
      <category>designpatterns</category>
      <category>javascript</category>
      <category>softwareengineering</category>
      <category>node</category>
    </item>
    <item>
      <title>The Ultimate Guide to TypeScript’s Utility Types</title>
      <dc:creator>Ketan Jakhar</dc:creator>
      <pubDate>Mon, 14 Oct 2024 12:04:50 +0000</pubDate>
      <link>https://forem.com/codexstoney/the-ultimate-guide-to-typescripts-utility-types-o19</link>
      <guid>https://forem.com/codexstoney/the-ultimate-guide-to-typescripts-utility-types-o19</guid>
      <description>&lt;p&gt;&lt;a href="https://codexstoney.medium.com/the-ultimate-guide-to-typescripts-utility-types-55db0cfb72e1?source=rss-b6d62e9eccec------2" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffosv8xjwo46hg948rovw.png" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Unlock the full potential of TypeScript by mastering its comprehensive set of utility types.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codexstoney.medium.com/the-ultimate-guide-to-typescripts-utility-types-55db0cfb72e1?source=rss-b6d62e9eccec------2" rel="noopener noreferrer"&gt;Continue reading on Medium »&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tips</category>
      <category>typescript</category>
      <category>react</category>
    </item>
    <item>
      <title>JavaScript Is Broken!</title>
      <dc:creator>Ketan Jakhar</dc:creator>
      <pubDate>Fri, 11 Oct 2024 08:49:13 +0000</pubDate>
      <link>https://forem.com/codexstoney/javascript-is-broken-5bop</link>
      <guid>https://forem.com/codexstoney/javascript-is-broken-5bop</guid>
      <description>&lt;p&gt;🚨 JavaScript is broken! It can be full of surprises, even for seasoned developers! 😅 Whether it’s strange type conversions, automatic semicolon insertion, or the infamous floating-point math issue, these quirks can turn debugging into a real headache. But don’t worry, I’ve got you covered!&lt;/p&gt;

&lt;p&gt;I just published an article on Medium breaking down some of the most common JavaScript pitfalls and how you can avoid them to write cleaner, more reliable code. 💻✨Read the full article here: &lt;a href="https://codexstoney.medium.com/javascript-is-broken-8841df6f6fc8?sk=12a9f6601c827148be64c736a3032a91" rel="noopener noreferrer"&gt;https://codexstoney.medium.com/javascript-is-broken-8841df6f6fc8?sk=12a9f6601c827148be64c736a3032a91&lt;/a&gt; 🔗&lt;/p&gt;

&lt;p&gt;In the article, you'll learn: 1️⃣ Why you should always specify the radix with parseInt() 2️⃣ How automatic semicolon insertion can give unexpected results 🛑 3️⃣ Why 0.1 + 0.2 !== 0.3 (and how to handle it!) 4️⃣ And more!&lt;/p&gt;

&lt;p&gt;Let’s keep the conversation going! Have any funny or frustrating JavaScript stories? Drop them in the comments! 💡👇&lt;/p&gt;

&lt;h1&gt;
  
  
  JavaScript #TypeScript #DevLife #Medium
&lt;/h1&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>typescript</category>
      <category>react</category>
    </item>
    <item>
      <title>Real-Time Data Processing with Node.js, TypeScript, and Apache Kafka</title>
      <dc:creator>Ketan Jakhar</dc:creator>
      <pubDate>Tue, 08 Oct 2024 12:20:28 +0000</pubDate>
      <link>https://forem.com/codexstoney/real-time-data-processing-with-nodejs-typescript-and-apache-kafka-3702</link>
      <guid>https://forem.com/codexstoney/real-time-data-processing-with-nodejs-typescript-and-apache-kafka-3702</guid>
      <description>&lt;p&gt;&lt;a href="https://codexstoney.medium.com/real-time-data-processing-with-node-js-typescript-and-apache-kafka-24a53f887326?source=rss-b6d62e9eccec------2" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgy3rnsqcd8w11vgxqsdk.png" alt="Apache Kafka" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Imagine orchestrating a symphony where each instrument plays in perfect harmony, even though they’re scattered across the globe. Sounds…&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codexstoney.medium.com/real-time-data-processing-with-node-js-typescript-and-apache-kafka-24a53f887326?source=rss-b6d62e9eccec------2" rel="noopener noreferrer"&gt;Continue reading on Medium »&lt;/a&gt;&lt;/p&gt;

</description>
      <category>queue</category>
      <category>pubsub</category>
      <category>node</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
