7

Singleton 单例模式简介与 C# 示例【创建型】【设计模式来了】 - 橙子家

 1 year ago
source link: https://www.cnblogs.com/czzj/p/SJMSLL_Singleton.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Singleton 单例模式简介与 C# 示例【创建型】【设计模式来了】


1、什么是单例模式?

一句话解释:

  单一的类,只能自己来创建唯一的一个对象。

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

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

 一个比喻:(班主任与学生)

  比如一个班级,只有一个班主任,任何一个同学要找班主任,都是找的同一个,班主任忙的时候,当然就出现排队的情况。

2、优缺点和使用场景

  • 优点:内存里只有一个实例,减少了内存的开销,也避免了对象高频创建带来的性能损耗。
  • 缺点:任务量大时,会出现排队,耗时增加。另外与单一职责原则冲突,一个类应该只关心内部逻辑,而不关心外面怎么样来实例化。

 使用场景举例:

  • 要求生产唯一序列号。
  • WEB 中的计数器,不用每次刷新都在数据库里加一次,用单例先缓存起来。
  • 创建的一个对象需要消耗的资源过多,比如 I/O 与数据库的连接等。

一、单例模式简单实现

public class Singleton{ private static Singleton instance = null; private static object lockObject = new object(); /// <summary> /// 私有化构造函数,防止外部实例化 /// </summary> private Singleton() { } public static Singleton Instance { get { if (instance == null) { lock (lockObject) // 线程同步锁 { if (instance == null) // Lazy Initialization { instance = new Singleton(); } } } return instance; } } /// <summary> /// 重置 Singleton /// </summary> public void Reset() { instance = null; }}

 测试代码:

static void Main(string[] args){ var instance1 = Singleton.Instance; var instance2 = Singleton.Instance; Console.WriteLine(instance1 == instance2); // 输出 true }

二、带参数的单例模式实现

public class SingletonParameters{ private static SingletonParameters instance = null; private static object lockObject = new object(); private int _firstvalue, _secondvalue; /// <summary> /// 私有化构造函数,防止外部实例化 /// </summary> private SingletonParameters(int first, int second) { this._firstvalue = first; this._secondvalue = second; } public static SingletonParameters InstanceParameters(int first, int second) { if (instance == null) { lock (lockObject) // 线程同步锁 { if (instance == null) // Lazy Initialization { instance = new SingletonParameters(first, second); } } } else { instance.FirstValue = first; instance.SecondValue = second; } return instance; } public int FirstValue { get { return _firstvalue; } set { _firstvalue = value; } } public int SecondValue { get { return _secondvalue; } set { _secondvalue = value; } } /// <summary> /// 重置 Singleton /// </summary> public void Reset() { instance = null; }}

 测试代码:

var instance1 = SingletonParameters.InstanceParameters(1, 2);Console.WriteLine($"FirstValue:{instance1.FirstValue}");Console.WriteLine($"SecondValue:{instance1.SecondValue}");var instance2 = SingletonParameters.InstanceParameters(3, 4);Console.WriteLine($"FirstValue:{instance2.FirstValue}");Console.WriteLine($"SecondValue:{instance2.SecondValue}");Console.WriteLine($"instance1 == instance2 : {instance1 == instance2}");

 

1868241-20230525184049411-1233134165.png

参考:https://www.cnblogs.com/gaochundong/p/design_pattern_singleton.html


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK