Learn Node.js design patterns, The singleton problem

Sometimes you need to make sure that you have one and only one instance of an object. This is where the singleton pattern can be useful. A singleton represents a single instance of an object. Only one can be created, no matter how many times the object is instantiated.

If there’s already an instance, the singleton will create a new one. Let’s take a look at where creating multiple instances of one object might create problems within our application. If you go to your exercise files for chapter two, lesson one, within the Start folderyou will notice that there is a logger.js file.

Within this file, we create a class called Logger. The idea is that we want our application modules to use this logger class instead of using console log directly. This logger saves information about all of the logs that are sent to it and it also logs each message with a timestamp. So once we create an instance of this object we can use the log method, send it a message, and it will log the timestamp and the message to the terminal as well as save information about that log. We also have a store.js file. Now if you look at this file we actually use the Logger.

On line one, we import the Logger class, and on line three we create a new instance called Logger so that we can actually use this class. So on line 10, within the store constructor we will log a new message every time a store is created. So it says New Store and it gives us the name of the store and how many items are in stock. We also have a shopper.js file. And if you take a look at the shopper.js file it also uses the Logger, and on line 3 it creates its own instance of the Logger. So, whenever we create new shoppers on line 10 within the constructor, we will log a message to the console that says a new shopper has been created,this is their name and this is how much money they have in their account.

Finally, the index.js, the main entry point for our application. Within our index.js, we are also using the Logger. On line one we import the class, and on line five we create our third instance of the Logger. Now, this file also uses the store and shopper classes and on line nine we create a new instance of a shopper and on line 10 we’ll create a new instance of a store.Now notice on line seven, we are sending messages to the Logger. So, on line seven we’re going to log the message starting app and on line 23 we’re also going to log the message finished config.

Now down here on line 25 of this file we’re using console log just to dump some debugging information. So here we’ll see how many messages have been saved inside of our Logger instance and on line 26 we’ll actually log each of those messages by mapping the logs array.So, the Logger instance stores an array called logs. We’re going to map over each item in that array, and we’re going to log the message that’s saved there to the console. Let’s go ahead and go out to our console and run this application. So if we run node index.js, and again I am in chapter two, lesson two one within the start folder, we will notice that we see four logs.

So we see our main application, log, starting the app along with its timestamp. We see that a new shopper has been created, a new store has been created, and then our main application has also finished running the configuration. The problem is, is because we have three instances of the Logger, we’re only looking at the information for the main instance. So in our debugging information down here we see two logs total and those logs are only starting app and finished config. Now the reason is, is whenever we looking at the current Logger instance, we’re only looking at the instance that was created within our main application.

Not the instance that was created within the shopper and the store. When we have this type of a problem a singleton can really come in handy.

Leave a Reply

avatar
1000
  Subscribe  
Notify of