This is an example of a law that is implemented using the single term pattern, so we said previously
that we cannot create two instances of the same object if we implement the Singleton pattern.
So basically, we need to define this instance.
The only instance that the object can can can take and instantiate, too.
So here you can see that this we have this object here that this private is the type of the longer and
the name is easy to interpret, to see what it is about.
And we call reinitialize that with the value that is returned by our implicit constructor here.
The constructor needs to be private because we don't want to allow the client to create objects using
using it.
So that's why in Singleton you need to have a private constructor and only public method here.
That is part of the Singleton implementations that get instance, a method which will return this instance
object to the client.
This needs to be made public because it's the only way to get a grasp of this object.
Otherwise you cannot access it at all.
And those two Modesto's those less last methods here are actually the actions that the longer can perform.
So basically it will output two lines in the console when the longer when the program is certain, when
the program is stopped, you may notice that you may have noticed here this comment.
So basically, this is the Eger initialization method for implementing a singleton.
And this means that the object is preloaded.
So it's not it's not loaded whenever you want it to to be used.
So whenever you call it, actually, it will be preloaded in your program.
So if you have a memory problem in your program, if if your application is consuming a lot of memory,
you may need to drop this implementation.
So I will come in this up and use lazy desalinisation method.
So I will I will show you how to do this.
So basically, we need to copy this instance here.
We do not initialize it.
We will leave it as it is.
And here in the get instance method, we will make it a bit more a bit more complicated, let's say.
So if this is a recipe, we need to ensure that the instance is not know if it is no, then we will
create.
But Singleton, no single, no longer here.
And also to add this single to Lowgar, object to the instance itself, and then we will return Dickinsons,
this is the and this is a way to make it to make our program more memory efficient.
So if the program will not need a single object at all, it will create the instance for it.
Whereas for the Icarian sanitisation, you will have it preloaded in your program.
And it's not that memory efficient to use this type of organization.
Another aspect that you may take care of that you may need to take care of is having your Singleton
object thread safe.
So if you are having a multithreaded, you are working on multithreaded application and two threads
will try to access your Singleton object at the same time, it may result in an exception.
So basically to make it safe, you just need to put the key synchronous here and there you have it.
This is a singleton that is you know that it's a synchronize, which means it's safe.
So this is these are some aspects that you really need to think of when implementing your singleton
object.
If you do not have any issues with the memory in your program, you can use securitization to make it
look a lot simpler.
And if you're not using multi trending, if you if your program is not operating in a multi threaded
environment, then you may need to drop the synchronized key here.
But otherwise this is the way to go.
So let's see this thing actually here.
I created here a Singleton logger by calling the get instanced method.
So like I said, the getting system is the only way to get the object, the instance that you need of
your other instance of your object.
So this will get our Singleton logger here and then we will output two messages for start and stop.
So let's see this in action.
Right, so you can see it's behaving as expected, so this is the single implementation.
Join me in the next video.
We are going to discuss about the builder father.