02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Singleton Design Pattern

The singleton design pattern restricts instantiation to a single instance and enables a delayed instantiation. This enables a public constant API to retrieve the instance while validating that only one instance of it can ever be created. Making it easy to share the object with multiple objects.

Video Transcript

00:00Welcome back.
00:02In section two, we're gonna go ahead
00:04and actually go right into those
00:06group of four design patterns,
00:09and the first group among them
00:10is the creational design patterns.
00:13The creational design patterns are design patterns
00:15that relate to the creation of objects.
00:19The first one that we're gonna meet is the singleton
00:21that is intended for creations where we wanna
00:24intentionally only have one instance
00:27of that specific object.
00:29We're gonna talk more about that soon.
00:32About that is the factory design pattern,
00:34and the factory design pattern is for a process
00:36where we wanna have a factory that creates objects.
00:40Above that is gonna be the abstract factory,
00:42which is gonna be a factory that is
00:45a little bit more sophisticated,
00:47followed by a builder which is even more sophisticated.
00:50And we're gonna wrap up the creational design patterns
00:53taking a look at the prototype design pattern,
00:56one that is very native to JavaScript.
00:59So let's jump right into the first one,
01:01the singleton design pattern.
01:05The singleton design pattern is very unique
01:07and you need to make sure that you don't overuse it.
01:11The most important features that you wanna pay attention to
01:13is one is that you don't overuse it.
01:16The second is that singletons have a delayed instantiation.
01:20So in scenarios where you don't want
01:22some logic group to happen automatically
01:24as soon as the application starts,
01:26but you only want it to happen and initialize
01:29at a certain time, such as maybe data just came in
01:31and now it's time to start this object.
01:33So one of the features of a singleton
01:35that you could control more
01:36exactly when you wanna instantiate it
01:38and it could be in completely different places
01:40cause the first time it's used
01:41is only when it's going to be instantiated.
01:44Keeping constant with it
01:46is that we need to have an interface
01:48that's easy to access anywhere.
01:49So it's a global interface in many ways
01:52that every item that needs to use this object
01:55will have direct access to it.
01:57Last but not least,
01:59there's only one instance of this object.
02:01So the rules are basically is that
02:03it's a delayed instantiation,
02:05it has a constant interface,
02:07and it could only have one instance in one application.
02:11So let's jump right into it
02:12and start building our first singleton
02:14and seeing it in action.
02:16So while you're away,
02:17I created a very simple structure
02:20that enables me to just add in elements,
02:23circles into our application.
02:25If you go into your starter file of section two,
02:28you'll find in your JavaScript
02:30a structure that should look familiar.
02:32We have here this module wrapper in line one through 12
02:35that is making sure that we're not in the global scope.
02:38And then we have here a jQuery ready command.
02:40And when that jQuery ready,
02:42when the document is ready,
02:43then we're gonna have a click event.
02:44And after that click event,
02:46we're gonna basically create a circle, position it,
02:49and then add it into the advert section.
02:52Now with this is starter file,
02:54I'm gonna go ahead and start changing
02:55almost everything inside of it
02:57because my goal is to turn this into a singleton.
03:00To do that, I'm gonna go ahead and create a new variable
03:03and I'm gonna call it circle generator singleton.
03:09Now my circle generator singleton
03:11is gonna basically only instantiate whenever we need it.
03:15To do that, I'm gonna go ahead and wrap around
03:17and create a module really,
03:19a module logic as we've seen in previous chapters
03:22and instantiate it.
03:24Now in this instantiation,
03:25I really don't want almost anything to happen.
03:28What I wanna happen is I wanna have a local internal reference
03:32inside of the circle generator,
03:34which will be called instance.
03:35Now the idea behind here is basically
03:39the only public things that are gonna be available here
03:41for the scope beyond here will be the instance itself
03:44and a function that will initiate the object.
03:47Now in this initiation object,
03:49we're literally gonna set and position everything.
03:52All the properties,
03:53all the methods are gonna live inside of here
03:54because we don't want them to execute and start
03:57until we're actually ready to create them.
04:01When we're ready to create them then
04:02and only then would we actually generate
04:04that initial item and return it.
04:07But before that, what we're gonna do
04:08is we're gonna return a public property,
04:12the only public property and return.
04:16And the only public method really
04:18is gonna be called get instance.
04:20Now the idea behind this get instance
04:22is basically whenever we wanna fetch this object,
04:25it will only then be created.
04:27And if it was already created,
04:28then we'll just refer back to the instance that we have.
04:32With that said, what we need to do
04:33is we need to ask ourselves, is that instance available?
04:36If it's not available, then let's go ahead and create it.
04:40So if the instance is not created yet,
04:42we're gonna go ahead and call the instantiation,
04:45which will then return to us our actual object,
04:48which we'll configure soon, return.
04:51It's gonna return to us the actual object itself
04:53or the public API of that object.
04:55And only what we want to be accessible
04:58would then become accessible.
05:00If it already has been created,
05:02or even if not because we've creating it
05:04if it didn't in the line 12 through 14,
05:07we're then gonna send ahead that instance,
05:09giving access to wherever public methods
05:12are inside of that init function.
05:14By creating this relatively complex structure,
05:17what we've done here is we made sure
05:19that only when we're actually gonna need it
05:21and we're gonna actually call it,
05:22only then the object will be created.
05:25With that said, let's go ahead and actually create the object
05:27so we could actually see it in action.
05:30To do that, I'm gonna go inside of that init area
05:32and literally I'm gonna create everything inside
05:35of that singleton, everything related to that singleton
05:37within the zone between line,
05:39basically inside of that init.
05:41Now, the properties that I'm gonna use
05:43is gonna be a circles,
05:45because I'm gonna create something
05:46a little bit more sophisticated.
05:48What I wanna do is I wanna create a reference
05:50so I could then create more complex things
05:52at a later stage, and with the word stage,
05:55I'm gonna go ahead here and also create a stage variable
05:58because I wanna also maybe in the future
06:00make sure that my advert could be changed
06:02to a different section.
06:03I'm not gonna do that right now in this lecture,
06:05but I'm preparing the groundwork
06:07for that type of possibility.
06:09Now that I have my private properties,
06:11it's time for me to create all the methods
06:13that I need for my singleton
06:15or for the management of a circle.
06:18So I'm gonna break down the steps
06:19instead of doing it all in one section here,
06:21I'm gonna break it to the creation
06:23for the positioning and for the addition of the elements.
06:27So let's start with the create process.
06:29So I'm gonna go ahead here and create inside of our init,
06:33create that create function.
06:36The create function will literally,
06:38all it really will do is just capture that creation
06:40that we've done here in line 30.
06:42So I'm gonna go ahead and chop that out,
06:45go right in there and put that for the create process.
06:48And I'm gonna go ahead and return that circle
06:50to wherever called.
06:52All right, so we have the creation process.
06:55The next step that we wanna do
06:56is we also wanna create a position function
06:59and I'm gonna put that above here.
07:00And the reason is because I'm gonna make it private
07:02just for the sake of the sample here.
07:04So the position function is gonna accept a few properties.
07:08One is gonna be the circle that we wanna position,
07:10the left and the top positions that we wanna push into.
07:14Now that I've created it, all that's left for me to do
07:16is first of all, I'm gonna go and just chop out
07:18the actual positioning that we have right here,
07:22put it all the way in the top here.
07:23So instead of positioning it the left,
07:26I'm gonna put here the left as the first property
07:29and the top as the second property.
07:31And that is my positioning.
07:33Now I'm gonna go ahead and also take advantage
07:35of the fact that I decided that the position
07:37is gonna be a private variable, a private elements.
07:40I'm gonna go here and just set all these stuff
07:42to be private.
07:43It's gonna make it very visible to me
07:44that these are not gonna be publicly accessible.
07:47And I'm gonna go ahead here and inside of the create function,
07:50I'm gonna call the position and position my new circle.
07:54And I'm gonna position it based on that left and top,
07:57making sure that when I create something,
07:59I'm gonna send in the left and top.
08:01So whenever I'm creating a new object,
08:02I'm gonna send its left and top positions.
08:05Beautiful, so we created the position,
08:07we created the create method.
08:09All that's left for me to do in the Singleton area
08:13is also to create the actual add itself.
08:16And the add itself is gonna basically accept a circle.
08:19And what is it gonna do?
08:20It's gonna basically approach the stage.
08:23It's gonna append that circle into the stage.
08:26What else are we gonna do?
08:27We also wanna add in that circle that we've just created
08:30into that array that we've created earlier,
08:33which was called a circle.
08:34And I'm gonna go ahead and push that circle
08:36so we have a reference back to it.
08:39Just for the sake of playing around with you,
08:41I'm gonna add here just one more quick function
08:44and I'm gonna call it index.
08:46And the index is literally just gonna tell me
08:48what index are we in.
08:49So I'm just gonna return right now the a,
08:53a circle, length, not left, length.
08:58And that will give me the current index that we're in
09:01because the a circle stores all of the information.
09:04Now that I've created all the methods
09:06and all the properties and the logic
09:07should be more or less in place,
09:09I really need to expose the properties that I wanna use.
09:12So I'm gonna go ahead and that's my next step
09:13and I'm gonna expose just like we did with the reveal pattern
09:17and I'm just gonna list out all the stuff
09:19that I wanna reveal.
09:20So create I wanna reveal
09:22and I wanna reveal the add as well.
09:25And I decided that I am not going to reveal the position
09:28and I'm gonna keep that private.
09:30All right, so now that I've created the Singleton
09:32and everything there is configured,
09:33all that's left for me is to test it out
09:35and to test that what I'm gonna do
09:37is I'm gonna go back into this structure that I have here
09:40and I'm not gonna change the click here
09:42but what I'm gonna do is I'm gonna just get rid
09:44of everything here and inside of my click
09:46I'm gonna start configuring the logic that I wanna set.
09:49So the first step, once our element is ready,
09:53I wanna get a reference inside of my click area.
09:56I wanna get a reference to my Singleton.
09:58Now I'm gonna call it cj for circle generator Singleton
10:03and I'm gonna reference my circle generator Singleton
10:06and get that instance.
10:07Now only in this stage, whenever a click happens,
10:10only then I'm creating the instance,
10:12making sure that I'm pushing the instantiation
10:14to a really late step in the process.
10:16So I don't have to trigger and create all the logic here
10:20that is there, it's just gonna sit there
10:21and wait for it to be instantiated.
10:24That's a great advantage already at this stage.
10:27The next step I'm gonna go ahead
10:28and I'm gonna go using that process,
10:31I'm gonna go and create a new circle.
10:33I'm gonna call this circle
10:36and I'm gonna use my CG to create a new circle
10:39and I wanna configure its position
10:41and because we're inside of this event,
10:42I'm gonna go ahead and send my page X minus 25
10:46because the radius in my CSS is 25
10:49and I'm gonna do the page Y as well.
10:52And there we go, we just created a new circle.
10:55All that's left for us to do in the next stage
10:57is we created it, all that's left for us to do
11:00is the CJ add and add that circle in
11:02so it's gonna be registered and also added onto the stage.
11:06Let's go ahead and test it out and see,
11:07first of all, if we have any errors
11:08before we continue forward,
11:10so I'm gonna click here on refresh
11:12and if I just click around,
11:13we could see that everything is still working.
11:15Our construct is a little bit more complicated
11:18and you might ask yourself,
11:19well, why would I wanna create something so complicated?
11:21And the answer is you probably wouldn't wanna do this
11:23for every single thing.
11:25But when we're working with a singleton,
11:26we have an assumption of two things.
11:28One is we wanna instantiate things late in the game.
11:31And the second thing is we wanna also make sure
11:33that it's accessible to other elements.
11:35So for example, if we wanted to create now
11:38another logic set here, so let me just go ahead here
11:41and I'm gonna create another logic set.
11:42So after the document is ready,
11:44let's say I also wanna make sure
11:46that there's some sort of a key press event going on.
11:49So I'm gonna go to my document, key press
11:53and I'm gonna go ahead and just quickly create
11:55that key press event.
11:57And, oh, that shouldn't be equal
11:59but we need to wrap this around.
12:02Okay, so we created the key press
12:04and I'm just gonna add here a simple logic.
12:06I'm gonna ask if the event that just happened
12:09and the key that was just pressed equals A
12:12and only if it equals A, then I'm gonna go ahead,
12:14I'm gonna get a reference to my CG
12:17for my circle generator singleton.
12:19It's not gonna create a new one if it's already created
12:21but if it's the first time around,
12:22then it will create it.
12:24Now next up, I'm just gonna go ahead
12:25and just copy this logic here
12:27but instead of setting the position X and Y,
12:29I'm just gonna go ahead here and just create here
12:31some sort of math floor
12:34and inside of there, I'm just gonna do a math random
12:38and I'm gonna duplicate that by 600.
12:41Let me just break a line here
12:43and I'm just gonna keep it to the 600 region.
12:46So I'm just gonna go ahead here and just copy this.
12:48It's gonna be in a squared region
12:49of up to 600 pixels by 600 pixels.
12:53All right, all that's left for me to do now
12:54is to actually test it out
12:56and if I save this, go back into my application
12:58and click on refresh,
13:00I'm gonna go ahead and click on the A character
13:03and if, let me just scroll up here
13:04and I'll click on the A character,
13:05you'll see that now it's gonna add
13:07as I'm adding it with the A
13:09and if I'm clicking onto the element into my stage area,
13:12then I'm also adding the elements
13:13and this is a perfect example
13:15of a usage of the singleton pattern.
13:18You really wanna stay away
13:20from overusing the singleton pattern,
13:22really a good place is where
13:24you only have one instance
13:25and you need it to be available
13:27to a lot of different objects
13:28or a lot of different scenarios
13:30and it's a big advantage also
13:31if you wanna instantiate it later down the line
13:34but create all those things in an organized way
13:36as we've done in this lecture.
13:39In this lecture, we created a singleton design pattern.
13:43In the next one, we're gonna take our step away
13:45from the instantiation of one item
13:48into a factory that creates a lot of items.
13:51We're gonna see why is that useful
13:52and in what scenarios would we wanna use a factory?

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!