 Okay, we're now in the last group of design patterns object-oriented design patterns and those are the creation of patterns So how do we go about creating instances and the first one? We will discuss is the so-called singleton pattern, which is all about Creating a single Instance of something and that's often the case if you want to control something So you want to make sure there's for example only one end point of a component only one interface that Communicates with other components. You don't want to have this double because they might have access to a restricted resource For example, if your component is in the network They have to listen they have to react to any kind of calls that come at a certain address and at this address. It can only be one Component one interface at a time So if you would have multiple of these controllers They would probably kind of restrict themselves in the excess or you have access to a memory location And only one at a time can read or write that's when you want to Maybe consider the singleton now. There's a lot of stuff going on here, even though it looks very simple and I'll Walk you through that. So first of all You should notice that The instance of a singleton so this class has an instance of itself, but this is a class attribute So there's only one of these for all the classes And even if you don't have an instance of singleton this Attribute exists for the class. So that's for example static in many programming languages The same goes for the method they get instance method that returns a singleton is also a class method it static You can call it even if there is no instance and then the third thing that's going on here is the constructor The constructor of the singleton is private. So In most programming languages, that means you cannot call this which means you cannot Create a new instance by for example doing new singleton This would in most programming languages not be possible because the singleton constructor is private The way this works is that instead you call get instance So for example in programming languages like Java this would look like singleton with a large s dot get instance so you just call it on the class level without creating a a new object and This one will then return you an object and the way it works is simply that this is a class method It checks whether the class attribute instance whether this one is set already If it's not for example, if it's now or undefined again depending on the programming language if it's not then It calls the constructor and it can't it can call the constructor because It's private. It's within the class. So this method is actually able to create a new instance This method is able to call new singleton. So if the instance undefined it calls this method It gets a new instance it returns it if this one is not undefined It simply returns the existing instance. So that way it makes sure that the first time this one is called We are creating one and afterwards. We're just returning it. There's always just one Now I said you might want to consider this and that's because there is a lot of criticism surrounding the singleton pattern So I think it's important to discuss that here I said before most patterns change over time or they might not be appropriate anymore They might be controversial. This is definitely one of them and the reason is Well manifold there are many reasons for this first of all one issue we have here is actually that we are violating the single responsibility because What the singleton pattern does is actually two things it makes sure there's only one instance But it also provides a global access to for example this one to the singleton instance to the controller So it actually has two responsibilities It violates single responsibilities The other thing is that this is very difficult to test it has an own state So the state depends on whether the method has been called or not and Depending on what other methods this might have the state changes over time and stateful Classes are generally not that easy to test. So that's a tricky thing here testing a singleton is hard Instance Access to restricted resources that I have mentioned as one of the reasons why you want to use this might be possible through other ways So it's not ideal to have a singleton if you can avoid it And a suggestion is very often to say well instead of the singleton maybe use something like dependency injection so make sure if you have to have something that has a state just provide the state by By a means of parameters for example to the constructor instead of Implementing a singleton. So it depends a bit on the use case that you're having But it's not always necessary to use this and if you can avoid it You should probably because then you don't have to test this anyhow Okay, so that's the singleton The next and the last pattern will go through in in this module is the factory pattern Which is a common way of as the name suggests Creating instances of the same thing