zoukankan      html  css  js  c++  java
  • Using .NET 4's Lazy<T> 实现单实例

    Using .NET 4's Lazy<T> type
     
    Explanation of the following code:

    1. If you're using .NET 4 (or higher) then you can use the System.Lazy<T> type to make the laziness really simple.
    2. All you need to do is pass a delegate to the constructor that calls the Singleton constructor, which is done most easily with a lambda expression.
    3. It also allows you to check whether or not the instance has been created with the IsValueCreated property.

    public sealed class Singleton

    {

        private Singleton()

        {

        }

        private static readonly Lazy<Singleton> lazy = new Lazy<Singleton>(() => new Singleton());

        public static Singleton Instance

        {

            get

            {

                return lazy.Value;

            }

        }

    }
     

    Example:
     
    The final example is here:

    namespace Singleton

    {

        class Program

        {

            static void Main(string[] args)

            {

                Calculate.Instance.ValueOne = 10.5;

                Calculate.Instance.ValueTwo = 5.5;

                Console.WriteLine("Addition : " + Calculate.Instance.Addition());

                Console.WriteLine("Subtraction : " + Calculate.Instance.Subtraction());

                Console.WriteLine("Multiplication : " + Calculate.Instance.Multiplication());

                Console.WriteLine("Division : " + Calculate.Instance.Division());

                Console.WriteLine(" ---------------------- ");

                Calculate.Instance.ValueTwo = 10.5;

                Console.WriteLine("Addition : " + Calculate.Instance.Addition());

                Console.WriteLine("Subtraction : " + Calculate.Instance.Subtraction());

                Console.WriteLine("Multiplication : " + Calculate.Instance.Multiplication());

                Console.WriteLine("Division : " + Calculate.Instance.Division());

                Console.ReadLine();

            }

        }

        public sealed class Calculate

        {

            private Calculate()

            {

            }

            private static Calculate instance = null;

            public static Calculate Instance

            {

                get

                {

                    if (instance == null)

                    {

                        instance = new Calculate();

                    }

                    return instance;

                }

            }

            public double ValueOne { get; set; }

            public double ValueTwo { get; set; }

            public double Addition()

            {

                return ValueOne + ValueTwo;

            }

            public double Subtraction()

            {

                return ValueOne - ValueTwo;

            }

            public double Multiplication()

            {

                return ValueOne * ValueTwo;

            }

            public double Division()

            {

                return ValueOne / ValueTwo;

            }

        }

    }

  • 相关阅读:
    Android实现简单的检测手机自由落体关闭屏幕
    Android Disable Package/Component 跳过app安装
    求单向链表中倒数第k个节点(c++):快慢指针/递归
    算法学习笔记---链表与数组
    pycharm使用matplotlib绘图学习笔记
    pycharm使用matplotlib绘制图像报错
    python numpy学习笔记
    python刷leetcode算法-- 左旋转字符串
    机器学习算法扫盲篇
    Kaggle入门篇
  • 原文地址:https://www.cnblogs.com/zeroone/p/3618634.html
Copyright © 2011-2022 走看看