• 设计模式之☞单例模式


    简介

    单例模式(Singleton Pattern)是 程序开发 中最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

    这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。

    注意:

    • 1、单例类只能有一个实例。
    • 2、单例类必须自己创建自己的唯一实例。
    • 3、单例类必须给所有其他对象提供这一实例。

    介绍

    意图:保证一个类仅有一个实例,并提供一个访问它的全局访问点。

    主要解决:一个全局使用的类频繁地创建与销毁。

    何时使用:当您想控制实例数目,节省系统资源的时候。

    如何解决:判断系统是否已经有这个单例,如果有则返回,如果没有则创建。

    关键代码:构造函数是私有的。

    优点:

    • 1、在内存里只有一个实例,减少了内存的开销,尤其是频繁的创建和销毁实例(比如管理学院首页页面缓存)。
    • 2、避免对资源的多重占用(比如写文件操作)。

    缺点:没有接口,不能继承,与单一职责原则冲突,一个类应该只关心内部逻辑,而不关心外面怎么样来实例化。

    案例一(无线程):

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 单例模式演示
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             Singleton obj =Singleton.CreateInstance();
    14             Singleton obj2 = Singleton.CreateInstance();
    15             Singleton obj3 = Singleton.CreateInstance();
    16             Console.ReadKey();
    17         }
    18         /// <summary>
    19         /// 当把一个类的构造函数的访问修饰符设为private后,那么这个类外部就不可以被创建对象了
    20         /// </summary>
    21         public class Singleton
    22         {
    23             /// <summary>
    24             /// 重写构造函数
    25             /// </summary>
    26             private Singleton()
    27             {
    28                 Console.WriteLine('.');
    29             }
    30             private static Singleton _instance;
    31             /// <summary>
    32             /// 创建实例
    33             /// </summary>
    34             /// <returns></returns>
    35             public static Singleton CreateInstance()
    36             {
    37                 if (_instance==null)
    38                 {
    39                     _instance = new Singleton();
    40                 }
    41                 return _instance;
    42             }
    43         }
    44     }
    45 }

    注:上述方法加入线程操作

    案例二(有线程单例模式处理,效率受点影响,必须等线程执行完,才会继续执行其他的线程)

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using System.Threading;
     7 
     8 namespace 单例模式之多线程
     9 {
    10     class Program
    11     {
    12         static void Main(string[] args)
    13         {
    14             for (int i = 0; i < 1000; i++)
    15             {
    16                 Thread t = new Thread(new ThreadStart(() =>
    17                 {
    18                     Singleton s = Singleton.CreateInstance();
    19                 }));
    20                 t.Start();
    21             }
    22             Console.WriteLine("ok");
    23             Console.ReadKey();
    24         }
    25         /// <summary>
    26         /// 当把一个类的构造函数的访问修饰符设为private后,那么这个类外部就不可以被创建对象了
    27         /// </summary>
    28         public class Singleton
    29         {
    30             /// <summary>
    31             /// 重写构造函数
    32             /// </summary>
    33             private Singleton()
    34             {
    35                 Console.WriteLine(".");
    36             }
    37             private static Singleton _instance;
    38             private static readonly object syn = new object();
    39             /// <summary>
    40             /// 创建实例
    41             /// </summary>
    42             /// <returns></returns>
    43             public static Singleton CreateInstance()
    44             {
    45                 lock (syn) //加锁
    46                 {
    47                     if (_instance == null)
    48                     {
    49                         _instance = new Singleton();
    50                     }
    51                 }
    52                 return _instance;
    53             }
    54         }
    55     }
    56 }

    案例三(推荐,既保持了单例模式,又提高了性能):

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using System.Threading;
     7 
     8 namespace 单例模式之多线程最优方法
     9 {
    10     class Program
    11     {
    12         static void Main(string[] args)
    13         {
    14             for (int i = 0; i < 1000; i++)
    15             {
    16                 Thread t = new Thread(new ThreadStart(() =>
    17                 {
    18                     Singleton s = Singleton.CreateInstance();
    19                 }));
    20                 t.Start();
    21             }
    22             Console.WriteLine("ok");
    23             Console.ReadKey();
    24         }
    25         /// <summary>
    26         /// 当把一个类的构造函数的访问修饰符设为private后,那么这个类外部就不可以被创建对象了
    27         /// </summary>
    28         public class Singleton
    29         {
    30             /// <summary>
    31             /// 重写构造函数
    32             /// </summary>
    33             private Singleton()
    34             {
    35                 Console.WriteLine(".");
    36             }
    37             private static Singleton _instance;
    38             private static readonly object syn = new object();
    39             /// <summary>
    40             /// 创建实例
    41             /// </summary>
    42             /// <returns></returns>
    43             public static Singleton CreateInstance()
    44             {
    45                 if (_instance == null)
    46                 {
    47                     lock (syn) //加锁
    48                     {
    49                         if (_instance == null)
    50                         {
    51                             _instance = new Singleton();
    52                         }
    53                     }
    54                 }
    55                 return _instance;
    56             }
    57         }
    58     }
    59 }

     项目链接:https://pan.baidu.com/s/1EpGurdSHqzUmEd7Ta6z7mQ 

    提取码:fd9s

  • 相关阅读:
    [python] 类组合与聚合关系
    [python] 伪私有属性,防止变量名冲突
    [vim] 配置文件之常用命令模式
    [VIM] 编辑器---多行注释和取消注释及多行复制和黏贴
    [Visual Studio Code] 执行python
    [C] 编译器codeblocks安装注意
    字符串全排列
    集合全量子集提取
    random函数详解
    Solr常用命令总结
  • 原文地址:https://www.cnblogs.com/chenyanbin/p/10908867.html
Copyright © 2020-2023  润新知