ISingletonDisplayer 单件模式显示器C#实现

很多情况下,我们需要一个Singleton窗体,比如,任务管理器就可以是windows应用系统中的一个Singleton,于是我们这样做:在窗体类的实现中添加一个静态指向单件的成员,提供一个静态的CreateInstance方法,当第一次调用此方法时创建单件。另外还要处理Closing事件,以使点击窗体右上角的“×”时,隐藏窗体。每次需要实现一个单件窗体时都要这么做,于是我决定写一个基类ISingletonDisplayer,一个窗体不用作任何改变,只需要将原来的基类Form改为ISingletonDisplayer就变成一个Singleton窗体,这就很方便了。先给出在VS2003中的实现。

public class ISingletonDisplayer : Form **//从Form继承
** {
private static ISingletonDisplayer singleton = null ;
private static bool toClean = false ;

protected ISingletonDisplayer()
{
this.Closing += new System.ComponentModel.CancelEventHandler(ISingletonDisplayer_Closing);
}

#region static for singleton

#region GetSingleton
public static ISingletonDisplayer GetSingleton()
{
if(ISingletonDisplayer.singleton != null)
{
ISingletonDisplayer.singleton.Visible = true ;
}
return ISingletonDisplayer.singleton ;
}

#endregion

#region ReCreateSingleton

//formOnUI存在是因为窗体只能在主线程(UI线程)中创建。
public static void ReCreateSingleton(Type targetType ,object[] args , Form formOnUI)
{
Type supType = typeof(ISingletonDisplayer) ;
if(! supType.IsAssignableFrom(targetType))
{
throw new Exception("Target type is not derived from ISingletonDisplayer !") ;
}

if(formOnUI.InvokeRequired)
{
object[] paras = {targetType ,args ,formOnUI} ;
formOnUI.Invoke(new CBackCreateForm(ISingletonDisplayer.ReCreateSingleton) ,paras) ;
}
else
{
try
{
ISingletonDisplayer.toClean = false ;
ISingletonDisplayer.singleton = (ISingletonDisplayer)Activator.CreateInstance(targetType ,args) ;
ISingletonDisplayer.singleton.Visible = false ;
}
catch(Exception ee)
{
throw ee ;
}
}
}
#endregion

#region DestroySingleton
public static void DestroySingleton()
{
ISingletonDisplayer.toClean = true ;
if(ISingletonDisplayer.singleton != null)
{
ISingletonDisplayer.singleton.Close() ;
ISingletonDisplayer.singleton = null ;
}
}
#endregion

#endregion

#region ISingletonDisplayer_Closing
private void ISingletonDisplayer_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if(! ISingletonDisplayer.toClean)
{
this.Visible = false ;
e.Cancel = true ;
return ;
}
}
#endregion
}

internal delegate void CBackCreateForm(Type targetType ,object[] args , Form formOnUI) ;

  • 使用方法:
  • 如果一个Displayer需要以Singleton模式呈现,那么可以先以常规的方式设计Displayer,设计过程中不需要涉及任何与单件
  • 相关的东西。设计完后,只要将Displayer的基类由Form改为ISingletonDisplayer即可。接下来即可以单件的模式使用Displayer了。
  • 如:
  • public class AppServersInfoForm2 : ISingletonDisplayer{}
  • object[] args = {obj} ;
  • ISingletonDisplayer.ReCreateSingleton(typeof(AppServersInfoForm2) ,args ,formOnUI) ;

上面的实现很容易理解,但是上面的实现有一个弊病,因为所有的单件窗体都要公用一个静态的ISingletonDisplayer singleton 成员,所以一个应用程序中只能有一个单件继承自ISingletonDisplayer。/ 在VS2005中可以使用泛型突破此限制。

下面在看看在VS2005中的实现。

public class ISingletonDisplayer

 1<t> : Form   
 2{   
 3private static ISingletonDisplayer<t> singleton = null ;   
 4private static bool toClean = false ; 
 5
 6protected ISingletonDisplayer()   
 7{   
 8this.Closing += new System.ComponentModel.CancelEventHandler(ISingletonDisplayer_Closing);   
 9} 
10
11public static ISingletonDisplayer<t> GetSingleton()   
12{   
13if(ISingletonDisplayer<t>.singleton != null)   
14{   
15ISingletonDisplayer<t>.singleton.Visible = true ;   
16}   
17return ISingletonDisplayer<t>.singleton ;   
18} 
19
20public static void ReCreateSingleton(Type targetType ,object[] args , Form formOnUI)   
21{   
22Type supType = typeof(ISingletonDisplayer) ;   
23if(! supType.IsAssignableFrom(targetType))   
24{   
25throw new Exception("Target type is not derived from ISingletonDisplayer<t> !") ;   
26} 
27
28if(formOnUI.InvokeRequired)   
29{   
30object[] paras = {targetType ,args ,formOnUI} ;   
31formOnUI.Invoke(new CBackCreateForm(ISingletonDisplayer<t>.ReCreateSingleton) ,paras) ;   
32}   
33else   
34{   
35try   
36{   
37ISingletonDisplayer<t>.toClean = false ;   
38ISingletonDisplayer<t>.singleton = (ISingletonDisplayer<t>)Activator.CreateInstance(targetType ,args) ;   
39ISingletonDisplayer<t>.singleton.Visible = false ;   
40}   
41catch(Exception ee)   
42{   
43throw ee ;   
44}   
45}   
46} 
47
48public static void DestroySingleton()   
49{   
50ISingletonDisplayer<t>.toClean = true ;   
51if(ISingletonDisplayer<t>.singleton != null)   
52{   
53ISingletonDisplayer<t>.singleton.Close() ;   
54ISingletonDisplayer<t>.singleton = null ;   
55}   
56}   
57  
58private void ISingletonDisplayer_Closing(object sender, System.ComponentModel.CancelEventArgs e)   
59{   
60if(! ISingletonDisplayer<t>.toClean)   
61{   
62this.Visible = false ;   
63e.Cancel = true ;   
64return ;   
65}   
66}   
67  
68}   
69
70
71internal delegate void CBackCreateForm(Type targetType ,object[] args , Form formOnUI) ; 
72
73如:   
74public class AppServersInfoForm2 : ISingletonDisplayer<appserversinfoform2>{}   
75object[] args = {obj} ;   
76ISingletonDisplayer<appserversinfoform2>.ReCreateSingleton(typeof(AppServersInfoForm2) ,args ,this) ; 
77
78这样每个单件都有自己的静态实例,就不会相互干扰了。 
79
80如果你有更好的实现方法,欢迎和我讨论</appserversinfoform2></appserversinfoform2></t></t></t></t></t></t></t></t></t></t></t></t></t></t></t></t></t>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus