Member-only story
Singleton Pattern
Creational Patterns
The Singleton Pattern is a design pattern that ensures a class has only one instance throughout the application’s lifecycle and provides a global point of access to that instance. It is commonly used when exactly one object is needed to coordinate actions across the system, such as managing database connections, logging, or configurations.
The InsurancePremiumCalculator class is created only once, and this single instance is shared throughout the application. The instance is stored in a static variable named _instance.
Private Constructor:
The constructor method is marked as private to prevent the creation of an instance from outside the class using the new keyword.
Static Access Method:
A static property called Instance provides a thread-safe way to access the Singleton instance. The use of lock ensures that multiple instances cannot be created simultaneously in a multithreaded environment.
Global Access:
The InsurancePremiumCalculator.Instance can be called from anywhere in the application to access the same instance.
/App
│
├── /App
│ └── /SingletonPattern
│ └── InsurancePremiumCalculator.cs
└── Program.cs







