首页 > 其他分享 >CSharp: Lazy Load Pattern in donet core 6

CSharp: Lazy Load Pattern in donet core 6

时间:2023-01-08 16:34:44浏览次数:52  
标签:Load core Lazy Pattern System Library using public

 

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

namespace Geovin.Du.DuLazyLoad.DuVirtualProxy
{

    /// <summary>
    /// Lazy Load Pattern
    /// </summary>
    public class College
    {

        /// <summary>
        /// 序号
        /// </summary>
        public int Id { get; init; }
        /// <summary>
        /// 图书馆信息
        /// </summary>
        public virtual Library? Library { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public void ShowDetails() =>
            Console.WriteLine(
                $"大学图书馆有 {Library!.NumberOfBooks} 本书 " +
                $"创建时间为:{Library.EstablishmentDate.ToShortDateString()}.");
    }

}

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

namespace Geovin.Du.DuLazyLoad.DuVirtualProxy
{
    /// <summary>
    /// Lazy Load Pattern
    /// </summary>
    public class CollegeFactory
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public College CreateFromId(int id) => new ProxyCollege(id);
    }

}


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

namespace Geovin.Du.DuLazyLoad.DuVirtualProxy
{

    /// <summary>
    /// 
    /// </summary>
    public class ProxyCollege : College
    {

        /// <summary>
        /// Lazy Load Pattern
        /// </summary>
        /// <param name="id"></param>
        public ProxyCollege(int id)
        {
            Id = id;
            Console.WriteLine("学院信息初始化完成.");
        }
        /// <summary>
        /// 
        /// </summary>
        public override Library? Library
        {
            get
            {
                if (base.Library == null)
                {
                    base.Library = new Library();
                }

                return base.Library;
            }
            set => base.Library = value;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object? obj)
        {
            if (obj is not College college)
            {
                return false;
            }

            return college.Id == Id;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode() => Id.GetHashCode();
    }

}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Geovin.Du.BuildingBlocks;


namespace Geovin.Du.DuLazyLoad.DuVirtualProxy
{

    /// <summary>
    /// Lazy Load Pattern
    /// </summary>
    public static class VirtualProxyExecutor
    {
        public static void Execute()
        {
            ConsoleExtension.WriteSeparator("延迟加载模式 Lazy Load Pattern--Virtual proxy demo");

            var collegeFactory = new CollegeFactory();
            var college = collegeFactory.CreateFromId(1);

            // College library shouldn't have been constructed before calling ShowDetails method.
            Console.WriteLine("给我更多关于这所大学的细节!");
            college.ShowDetails();
        }
    }

}

  

调用:

  Geovin.Du.DuLazyLoad.DuVirtualProxy.VirtualProxyExecutor.Execute();

  

输出:

延迟加载模式 Lazy Load Pattern--Virtual proxy demo
--------------------------------------------------
学院信息初始化完成.
给我更多关于这所大学的细节!
加载库...
大学图书馆有 210000 本书 创建时间为:2008/5/28.

  

标签:Load,core,Lazy,Pattern,System,Library,using,public
From: https://www.cnblogs.com/geovindu/p/17034821.html

相关文章