赞
踩
①单例模式的定义:是用来保证这个类在运行期间只会被创建一个实例,并提供一个访问它的全局访问点。
②单例模式的作用:保证一个类只有一个访问实例,节省系统资源。
③单例模式的特点:私有构造方法;私有成员变量;公有获取实例方法。
①懒汉式单例
- using System;
- using System.Collections.Generic;
- using System.Text;
-
- namespace SingleModel
- {
- //懒汉式单例模式
- public class Single
- {
- //私有构造
- private Single() { }
-
- //单例
- public static Single GetInstance()
- {
- return SingleHolder._Instance;
- }
-
- //内部类
- private static class SingleHolder
- {
- internal static readonly Single _Instance = new Single();
- }
-
- }//Class_end
- }
②饿汉式单例
- using System;
- using System.Collections.Generic;
- using System.Text;
-
- namespace SingleModel
- {
- //饿汉式单例模式
- public class Single2
- {
- private static readonly Single2 _Instance = new Single2();
- private Single2() { }
-
- public static Single2 GeInstance()
- {
- return _Instance;
- }
-
- }//Class_end
- }
③懒汉式单例
- using System;
- using System.Collections.Generic;
- using System.Text;
-
- namespace SingleModel
- {
- //懒汉式单例
- public class Single3
- {
- //定义一个保存实例的静态变量
- private static Single3 _Instance;
- //定义一个保证线程同步标识
- private static readonly object _Synchronized = new object();
- private Single3() { }
-
- public static Single3 GeInstance()
- {
- if (_Instance==null)
- {
- lock (_Synchronized)
- {
- //双重锁定
- if (_Instance==null)
- {
- _Instance = new Single3();
- }
- }
- }
- return _Instance;
- }
-
- }//Class_end
- }
④实现控制实例数目单例
- using System;
- using System.Collections.Generic;
- using System.Text;
-
- namespace SingleModel
- {
- //实现控制实例数目
- public class SingleExtand
- {
- //私有构造
- private SingleExtand() { }
-
- //定义一个缺省的key值前缀
- private static string _DefaultPreKey = "Cache";
- //记录当前实例
- public static int _SingleNum = 1;
-
- //定义实例的最大数目
- private static int _MaxSingleNum = 3;
-
- //定义一个保证线程同步的标识
- private static readonly object _Synchronized = new object();
- //缓存实例的容器
- private static Dictionary<String, SingleExtand> _SinleDic = new Dictionary<string, SingleExtand>();
-
-
- //本类实例
- public static SingleExtand GetInstance()
- {
- string key = _DefaultPreKey + _SingleNum;
- SingleExtand sinleExtand = null;
- if (_SinleDic.ContainsKey(key))
- {
- sinleExtand = _SinleDic[key];
- }
-
- if (sinleExtand==null)
- {
- lock (_Synchronized)
- {
- if (sinleExtand == null)
- {
- sinleExtand = new SingleExtand();
- _SinleDic.Add(key, sinleExtand);
- }
- }
- }
-
- _SingleNum++;
- if (_SingleNum>_MaxSingleNum)
- {
- _SingleNum = 1;
- }
-
- return sinleExtand;
- }
-
-
- }//Class_end
- }
- using System;
-
- namespace SingleModel
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("(线程安全)单例模式");
-
- Console.WriteLine("饿汉式单例1");
- Single single1 = Single.GetInstance();
- Single single2 = Single.GetInstance();
- Single single3 = Single.GetInstance();
- Console.WriteLine("single1:" + single1.GetHashCode());
- Console.WriteLine("single2:" + single2.GetHashCode());
- Console.WriteLine("single3:" + single3.GetHashCode());
- Console.WriteLine("\n");
-
- Console.WriteLine("懒汉式单例2");
- Single2 single21 = Single2.GeInstance();
- Single2 single22 = Single2.GeInstance();
- Single2 single23 = Single2.GeInstance();
- Console.WriteLine("single21:" + single21.GetHashCode());
- Console.WriteLine("single22:" + single22.GetHashCode());
- Console.WriteLine("single23:" + single23.GetHashCode());
- Console.WriteLine("\n");
-
- Console.WriteLine("懒汉式单例3");
- Single3 single31 = Single3.GeInstance();
- Single3 single32 = Single3.GeInstance();
- Single3 single33 = Single3.GeInstance();
- Console.WriteLine("single31:" + single31.GetHashCode());
- Console.WriteLine("single32:" + single32.GetHashCode());
- Console.WriteLine("single33:" + single33.GetHashCode());
- Console.WriteLine("\n");
-
-
- Console.WriteLine("控制实例数目的单例");
- SingleExtand singleExtand1 = SingleExtand.GetInstance();
- SingleExtand singleExtand2 = SingleExtand.GetInstance();
- SingleExtand singleExtand3 = SingleExtand.GetInstance();
- SingleExtand singleExtand4 = SingleExtand.GetInstance();
- SingleExtand singleExtand5 = SingleExtand.GetInstance();
- SingleExtand singleExtand6 = SingleExtand.GetInstance();
- Console.WriteLine("singleExtand1:" + singleExtand1.GetHashCode());
- Console.WriteLine("singleExtand2:" + singleExtand2.GetHashCode());
- Console.WriteLine("singleExtand3:" + singleExtand3.GetHashCode());
- Console.WriteLine("singleExtand4:" + singleExtand4.GetHashCode());
- Console.WriteLine("singleExtand5:" + singleExtand5.GetHashCode());
- Console.WriteLine("singleExtand6:" + singleExtand6.GetHashCode());
- Console.WriteLine("\n");
-
-
-
- Console.ReadLine();
- }
- }
- }
运行结果如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。