Now there is one last interesting feature which I also want to introduce and that would be private constructors.
Now what's a private constructor and which use does it have.
There is a pattern in object oriented programming which is called the Singleton pattern.
The Singleton pattern is about ensuring that you will always only have exactly one instance of a certain
class.
This can be useful in scenarios where you somehow can't use static methods or properties or you don't
want to.
But at the same time you want to make sure that you can create multiple objects based on a class but
that you always have exactly one object based on a class.
Let's say for our accounting department we want to make sure that we can only create exactly one object
based on this class because we have exactly one accounting department in our entire company.
We might have more than one I.T. department but we have exactly one accounting department now to enforce
this and to why did we manually call new accounting department multiple times.
We can turn the constructor of the accounting department class into a private constructor by adding
the private keyword in front of it.
Now what this does is it ensures that we can't call new on this.
Here you see I'm getting an error because the constructor is private so it's only accessible from inside
the class which sounds strange because how do we get inside of the class.
If we can't create objects based on it anymore.
The answer is well static methods a static method can be called on the class itself.
So you don't have to instantiated for that.
So here we can add a static method which we could call get instance that name is totally up to you though.
Now get instance will check if we already have an instance of this class and if not return a new one
for Dad we can add a new static property instance a static private property so you can put private in
front of static called instance which will be of type accounting department.
So in there we'll store an accounting department.
Instance so that's what I'm say here I have a static property which is accessible on the class itself
but only from inside the class.
And the value we store in there will be of type accounting department.
So off the class itself we can use this instance property here in get instance and check if this instance
is set here instead of static.
If we use this it will refer to the class itself and then we can access all our static properties on
that.
The alternative to that would be to use the class name and now if this is set I want to return this
instance or again class named after instance but this instead of a static method works it gives us access
to the class itself.
Then unlike this in a non static method which gives us access to the instance with which we're trying
to work not what we're doing here.
If however we don't make it in here then we have no instance yet.
Then I set this instance.
So this static instance property equal to new accounting department we can use that from inside here
because now we're inside of this class method.
So here we can access the private constructor and pass it in our I.D. And our reports array and then
return this instance here.
So now we're ever returning the one instance we might already have or if we don't have it yet we create
a new one.
But this code the marked code here can only run once because once we have a instance we make it into
this F block and we return the existing instance.
So now if you want to work with the accounting department instead of creating it like this we would
call const accounting accounting department dot get instance and this returns as a new instance of the
accounting department.
But if I do this again I will get the same instance as you will see if I cancel lock accounting and
accounting Q Here you will see that the two should be exactly equal.
If we save that and reload you see down there are my two accounting department objects.
They have the same I.D. the exact same setup.
They are the same object the same instance because we only have one instance with this singleton pattern
which is created with the help of the private keyword in front of the constructor.
Now this is arguably an approach which you won't use all the time.
The Singleton pattern can sometimes be useful.
You don't need it all the time but it's definitely worth to know about it because it is something interesting
which you can easily implement with typescript thanks to private constructors.