1
00:00:00,720 --> 00:00:05,160
All right guys so the next pattern we're going to look at is the singleton pattern which is actually

2
00:00:05,160 --> 00:00:12,420
a manifestation of the module pattern a singleton object is an immediate anonymous function and it can

3
00:00:12,420 --> 00:00:16,170
only return one instance of an object at a time.

4
00:00:16,170 --> 00:00:21,720
All right so repeated calls to the constructor will just return the same instance and like the module

5
00:00:21,720 --> 00:00:27,110
pattern it maintains a private reference which nothing from the outside can access.

6
00:00:27,120 --> 00:00:33,360
Now an example of why you may want to use a singleton is maybe you only want for instance one user object

7
00:00:33,360 --> 00:00:36,540
created at a time maybe a logged in user.

8
00:00:36,580 --> 00:00:41,250
It would prevent you from having two users from being created at once.

9
00:00:41,250 --> 00:00:47,190
Now oftentimes singletons are frowned upon because they give you a global point of access rather than

10
00:00:47,190 --> 00:00:53,700
embracing encapsulation and that bothers a lot of programmers and that they can also be kind of hard

11
00:00:53,700 --> 00:00:55,230
to debug debug.

12
00:00:55,230 --> 00:00:59,360
I think singletons do have their place but I haven't used them too much.

13
00:00:59,370 --> 00:01:04,860
I actually don't have much experience with this pattern at all but I wanted to include it in this section

14
00:01:05,130 --> 00:01:08,530
because it is a well-known pattern in singletons.

15
00:01:08,530 --> 00:01:14,910
They don't just relate to javascript but too many programming languages and just programming in general.

16
00:01:14,910 --> 00:01:21,390
So let's go ahead and let me give you a simple example here so I just have a blank js file and we're

17
00:01:21,390 --> 00:01:30,660
going to create a variable called Singleton and we're going to set this to our if we so will put our

18
00:01:30,660 --> 00:01:31,400
function

19
00:01:35,070 --> 00:01:46,110
and let's create a variable called instance and we're going to have a function called create instance.

20
00:01:46,560 --> 00:01:53,400
This isn't a very practical example but like I said I don't have much experience with it so create an

21
00:01:53,400 --> 00:01:56,220
object variable and will set it to a new object

22
00:01:59,220 --> 00:02:07,620
and we'll just we'll just pass in a string here and we'll just say object instance

23
00:02:10,510 --> 00:02:14,630
paying them will return that object.

24
00:02:14,710 --> 00:02:19,520
All right so now down here we're going to have our return just like the module pattern that I showed

25
00:02:19,510 --> 00:02:26,490
you in the last video and we're going to have our Get instance function here.

26
00:02:29,930 --> 00:02:34,580
OK and then what we're going to do is we're going to say if there is not an instance

27
00:02:38,450 --> 00:02:45,290
then we want instance to equal create instance

28
00:02:47,870 --> 00:02:53,120
and then under the if statement we're going to return the instance.

29
00:02:53,360 --> 00:02:59,960
All right and then down here what I'm going to do is instantiate an object I'm going to call it instance

30
00:03:00,730 --> 00:03:12,910
a spell I write instance a equals and then will say Singleton Daut get instance.

31
00:03:13,480 --> 00:03:20,000
OK so if we go ahead and console the log

32
00:03:22,460 --> 00:03:29,720
instance a and save you see we're going to get a string which is a primitive and it's an art says object

33
00:03:29,720 --> 00:03:32,050
instance.

34
00:03:32,050 --> 00:03:33,620
And I mean this doesn't have to be a string.

35
00:03:33,620 --> 00:03:34,340
We could do

36
00:03:40,230 --> 00:03:41,830
we could do that.

37
00:03:41,960 --> 00:03:44,360
That probably makes more sense.

38
00:03:44,360 --> 00:03:53,470
All right so if I go here if I try and instantiate another object from the singleton.

39
00:03:53,510 --> 00:04:01,460
So if I say instance b equals Singleton Daut instance and then I compare these two.

40
00:04:01,490 --> 00:04:09,620
So it's actually seeing one comment that the console dog blog.

41
00:04:10,130 --> 00:04:11,200
And we're going to say

42
00:04:15,200 --> 00:04:24,500
instance a is equal to instance B and save and you see we get true.

43
00:04:24,500 --> 00:04:29,320
All right so we can never have more than one instance even if we try.

44
00:04:30,020 --> 00:04:34,240
It's just going to give us the back the same instance.

45
00:04:34,250 --> 00:04:34,550
All right.

46
00:04:34,550 --> 00:04:39,440
So that's going to be it I'm not going to go much further into this because like I said I don't have

47
00:04:39,440 --> 00:04:44,660
a lot of experience with this pattern and I don't want to give you any wrong information just know that

48
00:04:44,690 --> 00:04:48,490
you can't have more than one instance with this pattern.

49
00:04:48,500 --> 00:04:53,810
All right so in the next video guys we're going to take a look at the factory pattern which is a pattern

50
00:04:53,810 --> 00:04:57,780
used to create objects multiple objects.

