zoukankan      html  css  js  c++  java
  • 单例模式

    CommonUtility文件中写这个方法
    /// 把类的构造函数访问权限设置为private,则该类不能在外界被new了
    /// 在当前类型中创建一个静态的方法,用该静态方法来返回一个对象
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace CommonUtility
     8 {
     9     public class Singleton<T> where T : class,new()
    10     {
    11         /// <summary>
    12         /// 单列模式
    13         /// </summary>
    14         /// <typeparam name="T"></typeparam>
    15         private static T _instance = null;
    16         public static readonly Object obj = new object();
    17         public static T Instance()
    18         {
    19             if (_instance == null)
    20             {
    21                 lock (obj)
    22                 {
    23                     return _instance = Activator.CreateInstance<T>();
    24                 }
    25             }
    26             return _instance;
    27         }
    28     }
    29 }
    在BLL或者DAL调用时需要先引用CommonUtility。

    1
    public static new CContractMgrBLL Instance 2 { 3   get { return Singleton<CContractMgrBLL>.Instance(); } 4 }

    作用:单例模式就是保证在整个应用程序的生命周期中,在任何时刻,被指定的类只有一个实例,并为客户程序提供一个获取该实例的全局访问点。

  • 相关阅读:
    绑定姿势
    Mesh.CombineMeshes
    Mono vs IL2CPP
    lua keynote2
    lua keynote
    游戏编程模式KeyNote
    架构、性能和游戏
    Canvas
    AssetBundle Manager
    Loading AssetBundle Manifests
  • 原文地址:https://www.cnblogs.com/chizhida/p/7298035.html
Copyright © 2011-2022 走看看