Singleton Pattern

Taylan Can Hardal
2 min readMar 31, 2022

Hey everybody I’ll explain the Singleton pattern, which is one of the most preferred design patterns in the industry. Singleton design pattern guarantees that only 1 object is created at runtime.

All patterns are a solution to a problem. They were created for problems. So first we need to know what the problem is. The Singleton Pattern is needed when only one object is required for the entire application. Why do we need only one object in the whole application?

If the operations of an object change only depending on the parameters, it is pointless to produce several of that object. Each produced object occupies a place in memory.

For these reasons, we need Singleton Pattern. Let’s look at the sample project.

Firstly, we created a class called Singleton. The Singleton should always be a ‘sealed’ class to prevent class inheritance through external classes and also through nested classes. In addition, we create a private constructor and prevent the creation of new instances with new.

In Program.cs, we can test whether our project works in accordance with the Singleton Pattern. If the Instances we have created are different from each other, it means that we have made a mistake somewhere. But if you try, you will find that they always turn out to be equal to each other.

References

Contact me :

bolŞans…

--

--

Taylan Can Hardal

Software Engineer @Turkcell. Savin’ the Earth. Somewhere