Singleton Pattern
Singleton comes under creation type design pattern. It restricts creation of object of a class to one object.
In this pattern, a single class is responsible for creating object. It makes sure only one object is getting created, by providing way to access that object directly without creating another object throughout the application.
Static Implementation
In this implementation, we create static property which returns instance of class and declare constructor as private so that it can be instantiated by another class.
Example
- public sealed class Maths {
- private Maths() {}
- private static Maths objMaths = null;
- public static Maths Instance {
- get {
- if (objMaths == null) {
- objMaths = new Maths();
- }
- return instance;
- }
- }
- public double ValueOne {
- get;
- set;
- }
- public double ValueTwo {
- get;
- set;
- }
- public double Add() {
- return ValueOne + ValueTwo;
- }
- public double Subtraction() {
- return ValueOne - ValueTwo;
- }
- public double Multiplication() {
- return ValueOne * ValueTwo;
- }
- public double Division() {
- return ValueOne / ValueTwo;
- }
- }
- }
Now, in the above example, we have created class "Maths" which has private constructor so that it can be instantiated by another class. And, it is having a static property which returns object of Maths class.

Former memberPosted Jun 2, 2017, 7:19 AM
How about double and triple ton pattern......write article on this if possible.