Q:

Singleton Design Pattern in .Net

0

Singleton Design Pattern in .Net

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Calculator
{
class Program
{
static void Main(string[] args)
{
Calculator.Instance.Num1 = 20;
Calculator.Instance.Num2 = 5;
Console.WriteLine("Addition : " + Calculator.Instance.Addition());
Console.WriteLine("Subtraction : " + Calculator.Instance.Subtraction());
Console.WriteLine("Multiplication : " + Calculator.Instance.Multiplication());
Console.WriteLine("Division : " + Calculator.Instance.Division());
Console.WriteLine("\n----------------------\n");
Calculator.Instance.Num2 = 20;
Console.WriteLine("Addition : " + Calculator.Instance.Addition());
Console.WriteLine("Subtraction : " + Calculator.Instance.Subtraction());
Console.WriteLine("Multiplication : " + Calculator.Instance.Multiplication());
Console.WriteLine("Division : " + Calculator.Instance.Division());
Console.ReadLine();
}
}

public class Calculator
{
private Calculator()
{
Console.WriteLine("Single Instance");
}
private static object syncLock = new object();
private static Calculator instance = null;
public static Calculator Instance
{
get
{
lock (syncLock)
{
if (instance == null)
{
instance = new Calculator();
}
}
return instance;
}
}
public double Num1 { get; set; }
public double Num2 { get; set; }
public double Addition()
{
return Num1 + Num2;
}
public double Subtraction()
{
return Num1 - Num2;
}
public double Multiplication()
{
return Num1 * Num2;
}
public double Division()
{
return Num1 / Num2;
}
}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now