Skip to main content

JavaScript - Singleton Pattern

The singleton design pattern is probably the simplest and most common pattern in JavaScript.

So why should you use the singleton pattern?
  • Encapsulation of members & functions
  • Creates its own Namespace
  • A singleton is a single instance object
  • Encourages code reuse
  • Improves readability because you can logically organise your code

The point of a singleton is to only have one instance. A shopping cart is a good example of something that you may want only a single instance of at one time.


The simplest form of Singleton is an object literal. This loose form of Singleton cannot be instantiated. All of the members are now accessible through the Singleton variable, accessible through dot notation.
varmyCart = {
   self: this,

   totalCost: 0,

   totalQty: 0,

   cart: {},

   getCart: function(){ },

   updateCart: function(){ }

};

alert( "Total cost: $"+ myCart.totalCost );

By using closures, we can create Private and Public Members and Functions. This pattern is more commonly called the Module Pattern.

 var myCart = function(){
    // Private

    var self = this;

    var totalCost = 0;

    var totalQty = 0;

    var cart = {};

    return {

        // Public

        getCart: function(){ },

        updateCart: function(){ }

    };

}();

Comments

  1. May be adding discussions about engineering softwares like latex or programming languages like C/C++/fortran etc could help.

    ReplyDelete
    Replies
    1. Thanks for the suggestion. But it would be difficult to maintain such discussion on blog. I feel that it would be better to start a forum for that. Consider stackoverflow.com, its a very nice platform to get everything answered properly and in less time

      Delete

Post a Comment

Popular posts from this blog

Ubuntu: Access a usb flash drive from the terminal

    1. Find what the drive is called You'll need to know what the drive is called to mount it. To do that fire off: sudo fdisk -l You're looking for a partition that should look something like:   /dev/sdb1 . Remember what it's called. 2. Create a mount point Create a new directory in   /media   so you can mount the drive onto the filesystem: sudo mkdir /media/usb 3. Mount! sudo mount /dev/sdb1 /media/usb When you're done, just fire off: sudo umount /media/usb Source: StackOverflow

Code for Php based online Treasure Hunt

Hello guys. Some time back I organized an online treasure hunt as part of an event at my college. I thought of sharing the code with you, as you might find it useful. So, I uploaded it on github and here is the link to my repository. Download it from here , and enjoy organizing the game

Java: Use BigInteger in for-loop

In my previous post , I mentioned a way to handle large integers by using BigInteger. Now I'm going to provide a very important usage of it. We often use for-loop. So here is the way to use it: Ordinary integers: for(int i = 1; i <= n; i++) {  //Task to do } BigInteger: for (BigInteger bi = BigInteger.valueOf(1);                 bi.compareTo(n) <= 0;                 bi = bi.add(BigInteger.ONE)) { //Task to do } here n is a BigInteger variable.