5-) Singleton Design Pattern

recep orhan
Jul 29, 2021

Singleton Design Pattern’s main purpose is to creating only one instance to one class and accessing it globally. Generally, it is used to keep under control to accessing sources like databases and files. Also, the Unit of Work Pattern that is used for changes that made the code side push to the database at one time, uses the Singleton Design Pattern. Let’s code it.

I created the class Singleton with a private constructor, then I created a public method for getting only one instance of the class. I made a thread-safe operation in the method with the locker object.

Let’s get two instances from Singleton class then see they are equal or not.

When run the app output will be this.

For DesignPattern practices with c#, you can look at my Github repo.https://github.com/orhanrecep90/DesignPatterns

--

--