My Singleton in C#
发表于:2007-06-30来源:作者:点击数:
标签:
//MySingleton using System; //SingletonPage Class class SingletonPage { //Fields protected static SingletonPage checkoutpage; //Constructor is protected to ensure Singleton protected SingletonPage() { Console.WriteLine(if you see this line,
//MySingleton
using System;
//SingletonPage Class
class SingletonPage
{
//Fields
protected static SingletonPage checkoutpage;
//Constructor is protected to ensure Singleton
protected SingletonPage()
{
Console.WriteLine("if you see this line,then the only one instence is created!");
}
//Use this to Create SingletonPage instance
public static SingletonPage NewCheckOutPage()
{
if (checkoutpage==null)
checkoutpage= new SingletonPage();
return checkoutpage;
}
};
//-------------------------------------End of SingletonPage Class
//TestApp
class TestApp
{
public static void Main(string[] args)
{
Console.WriteLine("@#create@# pagea:");
SingletonPage pagea=SingletonPage.NewCheckOutPage();
Console.WriteLine("@#create@# pageb:");
SingletonPage pageb=SingletonPage.NewCheckOutPage();
Console.WriteLine("@#create@# pagec:");
SingletonPage pagec=SingletonPage.NewCheckOutPage();
Console.WriteLine("@#create@# paged:");
SingletonPage paged=SingletonPage.NewCheckOutPage();
while(true){}
}
};
原文转自:http://www.ltesting.net