1
00:00:02,300 --> 00:00:10,320
Now there is one last interesting feature which I also want to introduce and that would be private constructors.

2
00:00:10,370 --> 00:00:14,700
Now what's a private constructor and which use does it have.

3
00:00:14,910 --> 00:00:20,330
There is a pattern in object oriented programming which is called the Singleton pattern.

4
00:00:20,490 --> 00:00:27,180
The Singleton pattern is about ensuring that you will always only have exactly one instance of a certain

5
00:00:27,180 --> 00:00:28,070
class.

6
00:00:28,080 --> 00:00:33,510
This can be useful in scenarios where you somehow can't use static methods or properties or you don't

7
00:00:33,510 --> 00:00:34,280
want to.

8
00:00:34,470 --> 00:00:39,790
But at the same time you want to make sure that you can create multiple objects based on a class but

9
00:00:39,780 --> 00:00:44,660
that you always have exactly one object based on a class.

10
00:00:44,670 --> 00:00:52,200
Let's say for our accounting department we want to make sure that we can only create exactly one object

11
00:00:52,200 --> 00:00:57,830
based on this class because we have exactly one accounting department in our entire company.

12
00:00:57,900 --> 00:01:04,190
We might have more than one I.T. department but we have exactly one accounting department now to enforce

13
00:01:04,200 --> 00:01:10,300
this and to why did we manually call new accounting department multiple times.

14
00:01:10,410 --> 00:01:16,380
We can turn the constructor of the accounting department class into a private constructor by adding

15
00:01:16,380 --> 00:01:18,900
the private keyword in front of it.

16
00:01:18,900 --> 00:01:23,500
Now what this does is it ensures that we can't call new on this.

17
00:01:23,520 --> 00:01:29,490
Here you see I'm getting an error because the constructor is private so it's only accessible from inside

18
00:01:29,490 --> 00:01:34,410
the class which sounds strange because how do we get inside of the class.

19
00:01:34,440 --> 00:01:37,950
If we can't create objects based on it anymore.

20
00:01:37,950 --> 00:01:43,650
The answer is well static methods a static method can be called on the class itself.

21
00:01:43,650 --> 00:01:46,270
So you don't have to instantiated for that.

22
00:01:46,290 --> 00:01:53,210
So here we can add a static method which we could call get instance that name is totally up to you though.

23
00:01:53,300 --> 00:01:59,540
Now get instance will check if we already have an instance of this class and if not return a new one

24
00:02:00,110 --> 00:02:08,000
for Dad we can add a new static property instance a static private property so you can put private in

25
00:02:08,000 --> 00:02:12,050
front of static called instance which will be of type accounting department.

26
00:02:12,050 --> 00:02:15,010
So in there we'll store an accounting department.

27
00:02:15,010 --> 00:02:21,210
Instance so that's what I'm say here I have a static property which is accessible on the class itself

28
00:02:21,450 --> 00:02:23,520
but only from inside the class.

29
00:02:23,610 --> 00:02:27,600
And the value we store in there will be of type accounting department.

30
00:02:27,630 --> 00:02:37,700
So off the class itself we can use this instance property here in get instance and check if this instance

31
00:02:37,700 --> 00:02:39,920
is set here instead of static.

32
00:02:39,920 --> 00:02:46,670
If we use this it will refer to the class itself and then we can access all our static properties on

33
00:02:46,670 --> 00:02:47,320
that.

34
00:02:47,420 --> 00:02:57,960
The alternative to that would be to use the class name and now if this is set I want to return this

35
00:02:57,960 --> 00:03:04,050
instance or again class named after instance but this instead of a static method works it gives us access

36
00:03:04,050 --> 00:03:05,120
to the class itself.

37
00:03:05,130 --> 00:03:11,040
Then unlike this in a non static method which gives us access to the instance with which we're trying

38
00:03:11,040 --> 00:03:13,600
to work not what we're doing here.

39
00:03:13,650 --> 00:03:17,340
If however we don't make it in here then we have no instance yet.

40
00:03:17,340 --> 00:03:19,670
Then I set this instance.

41
00:03:19,680 --> 00:03:27,960
So this static instance property equal to new accounting department we can use that from inside here

42
00:03:28,650 --> 00:03:32,060
because now we're inside of this class method.

43
00:03:32,070 --> 00:03:41,740
So here we can access the private constructor and pass it in our I.D. And our reports array and then

44
00:03:41,740 --> 00:03:44,890
return this instance here.

45
00:03:44,890 --> 00:03:50,200
So now we're ever returning the one instance we might already have or if we don't have it yet we create

46
00:03:50,200 --> 00:03:50,910
a new one.

47
00:03:50,980 --> 00:03:57,070
But this code the marked code here can only run once because once we have a instance we make it into

48
00:03:57,070 --> 00:04:00,400
this F block and we return the existing instance.

49
00:04:00,400 --> 00:04:05,380
So now if you want to work with the accounting department instead of creating it like this we would

50
00:04:05,380 --> 00:04:13,470
call const accounting accounting department dot get instance and this returns as a new instance of the

51
00:04:13,470 --> 00:04:14,850
accounting department.

52
00:04:14,850 --> 00:04:22,270
But if I do this again I will get the same instance as you will see if I cancel lock accounting and

53
00:04:22,270 --> 00:04:26,550
accounting Q Here you will see that the two should be exactly equal.

54
00:04:26,590 --> 00:04:34,080
If we save that and reload you see down there are my two accounting department objects.

55
00:04:34,150 --> 00:04:36,670
They have the same I.D. the exact same setup.

56
00:04:36,730 --> 00:04:43,210
They are the same object the same instance because we only have one instance with this singleton pattern

57
00:04:43,390 --> 00:04:47,950
which is created with the help of the private keyword in front of the constructor.

58
00:04:47,950 --> 00:04:52,460
Now this is arguably an approach which you won't use all the time.

59
00:04:52,460 --> 00:04:55,050
The Singleton pattern can sometimes be useful.

60
00:04:55,060 --> 00:05:00,220
You don't need it all the time but it's definitely worth to know about it because it is something interesting

61
00:05:00,220 --> 00:05:04,780
which you can easily implement with typescript thanks to private constructors.
