CSharp: Singleton Pattern

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/// <summary>
/// Summary description for Spooler.
/// geovindu,Geovin Du,涂聚文
/// 单例 模式(Singleton Patterns)
/// </summary>
public class Spooler
{
    /// <summary>
    ///
    /// </summary>
    private static bool instance_flag = false;
    /// <summary>
    ///
    /// </summary>
    private Spooler()
    {
    }
    /// <summary>
    ///
    /// </summary>
    /// <returns></returns>
    public static Spooler getSpooler()
    {
        if (!instance_flag)
            return new Spooler();
        else
            return null;
    }
 
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/// <summary>
/// Summary description for SingletonException.
/// geovindu,Geovin Du,涂聚文
/// 单例 模式(Singleton Patterns)
/// </summary>
public class SingletonException : Exception
{
    /// <summary>
    /// new exception type for singleton classes
    /// </summary>
    /// <param name="s"></param>
    public SingletonException(string s)
        : base(s)
    {
    }
}

  

调用测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/// <summary>
   /// geovindu,Geovin Du,涂聚文
   /// 单例 模式(Singleton Patterns)
   /// </summary>
   public partial class SingletonPatternsForm : Form
   {
 
       /// <summary>
       ///
       /// </summary>
       public SingletonPatternsForm()
       {
           InitializeComponent();
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void SingletonPatternsForm_Load(object sender, EventArgs e)
       {
 
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void button1_Click(object sender, EventArgs e)
       {
           Spooler sp1 = Spooler.getSpooler();
           if (sp1 != null)
           {
               this.textBox1.Text = "Got 1 spooler";
               Console.WriteLine("Got 1 spooler");
           }
           Spooler sp2 = Spooler.getSpooler();
           if (sp2 == null)
           {
               this.textBox2.Text = "Can\'t get spooler";
               Console.WriteLine("Can\'t get spooler");
           }
           else
           {
                
               this.textBox2.Text = "Got 2 spooler";
           }
           //fails at compile time 不可访问,受保护级别
          // Spooler sp3 = new Spooler();
 
 
       }
   }

  

posted @   ®Geovin Du Dream Park™  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2021-09-18 sql server: Parent/Child hierarchy tree view
2016-09-18 csharp: ODP.NET,System.Data.OracleClient(.net 4.0) and System.Data.OleDb读取Oracle g 11.2.0的区别
2015-09-18 sql:Oracle11g 表,视图,存储过程结构查询
2015-09-18 sql:MySQL 6.7 表,视图,存储过程结构查询
2010-09-18 csharp:DataRelation 对象访问相关数据表中的记录
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示