System.Lazy<T>延迟加载

/*
 * 用户: Changweihua
 * 日期: 2013/7/10
 * 时间: 8:39
 *
 * changweihua@outlook.com
 * http://www.cmono.net
 *
 */
using System;
using System.Threading;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
namespace TestLazy
{
    class Program
    {
        public static void Main(string[] args)
        {
            foreach (var element in Product.GetProductListLazy())
            {
                //下面的操作会执行Product类的构造函数
                Product p = element.Value;
                //构造函数执行完成
                int id = element.Value.Id;
                Console.WriteLine(id);
            }
               
            foreach (var element in Product.GetProductList())
            {
                //下面的操作会执行Product类的构造函数
                Product p = element;
                //构造函数执行完成
                int id = element.Id;
                Console.WriteLine(id);
            }
               
            Console.ReadKey(true);
        }
    }
       
    public class Product
    {
        public int Id { get; set; }
        public string Title { get; set; }
           
        public Product()
        {
            Thread.Sleep(5000);
        }
           
        public static IList<Lazy<Product>> GetProductListLazy()
        {
            Stopwatch s = new Stopwatch();
            try
            {
                Console.WriteLine("LazyLoad加载");
                s.Start();
                Lazy<Product> product1 = new Lazy<Product>(() => new Product() { Id = 1, Title = "产品 1" });
                Lazy<Product> product2 = new Lazy<Product>(() => new Product() { Id = 2, Title = "产品 2" });
                Lazy<Product> product3 = new Lazy<Product>(() => new Product() { Id = 3, Title = "产品 3" });
                return new List<Lazy<Product>> { product1, product2, product3 };
            }
            finally {
                s.Stop();
                Console.WriteLine("耗时 {0}", s.Elapsed.TotalSeconds);
            }
        }
        public static IList<Product> GetProductList()
        {
            Stopwatch s = new Stopwatch();
            try
            {
                Console.WriteLine("非LazyLoad加载");
                s.Start();
                Product product1 = new Product() { Id = 1, Title = "产品 1" };
                Product product2 = new Product() { Id = 2, Title = "产品 2" };
                Product product3 = new Product() { Id = 3, Title = "产品 3" };
                return new List<Product> { product1, product2, product3 };
            }
            finally {
                s.Stop();
                Console.WriteLine("耗时 {0}", s.Elapsed.TotalSeconds);
            }
        }
           
        /// <summary>
        /// 保证有一个实例,同时实现延迟加载,按需加载
        /// </summary>
        public class SingletonLazy
        {
            private static Lazy<SingletonLazy> _Instance = new Lazy<SingletonLazy>();
       
            public static SingletonLazy GetInstance()
            {
                return _Instance.Value;
            }
        }
    }
       
}


知识共享许可协议
《System.Lazy<T>延迟加载》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

1 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     163.95ms