商业源码热门下载www.html.org.cn
很多情况下,我们需要一个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<t> : form
{
private static isingletondisplayer<t> singleton = null ;
private static bool toclean = false ;
protected isingletondisplayer()
{
this.closing += new system.componentmodel.canceleventhandler(isingletondisplayer_closing);
}
public static isingletondisplayer<t> getsingleton()
{
if(isingletondisplayer<t>.singleton != null)
{
isingletondisplayer<t>.singleton.visible = true ;
}
return isingletondisplayer<t>.singleton ;
}
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<t> !") ;
}
if(formonui.invokerequired)
{
object[] paras = {targettype ,args ,formonui} ;
formonui.invoke(new cbackcreateform(isingletondisplayer<t>.recreatesingleton) ,paras) ;
}
else
{
try
{
isingletondisplayer<t>.toclean = false ;
isingletondisplayer<t>.singleton = (isingletondisplayer<t>)activator.createinstance(targettype ,args) ;
isingletondisplayer<t>.singleton.visible = false ;
}
catch(exception ee)
{
throw ee ;
}
}
}
public static void destroysingleton()
{
isingletondisplayer<t>.toclean = true ;
if(isingletondisplayer<t>.singleton != null)
{
isingletondisplayer<t>.singleton.close() ;
isingletondisplayer<t>.singleton = null ;
}
}
private void isingletondisplayer_closing(object sender, system.componentmodel.canceleventargs e)
{
if(! isingletondisplayer<t>.toclean)
{
this.visible = false ;
e.cancel = true ;
return ;
}
}
}
internal delegate void cbackcreateform(type targettype ,object[] args , form formonui) ;
如:
public class appserversinfoform2 : isingletondisplayer<appserversinfoform2>{}
object[] args = {obj} ;
isingletondisplayer<appserversinfoform2>.recreatesingleton(typeof(appserversinfoform2) ,args ,this) ;
这样每个单件都有自己的静态实例,就不会相互干扰了。
如果你有更好的实现方法,欢迎和我讨论
新闻热点
疑难解答