单例模式

----------------------------20150123---------------------------------------
单例模式
1、将构造函数私有化。
2、提供一个静态方法,返回一个对象。
3、创建一个单例

public partial class Form2:Form
{
//全局唯一的单例
public static Form2 FrmSingle=null;

private Form2()
{
InitializeComponent();
}

public static Form2 GetSingle()
{
if(FrmSingle == null)
{
FrmSingle frm2=new Form2();
}

return FrmSingle;
}
}

private void button1_Click(object sender,EventArgs e)
{
Form2 frm2=Form2.GetSingle();
frm2.Show();
}

 

posted @ 2015-01-23 14:24  teacherzj  阅读(177)  评论(0编辑  收藏  举报