My Singleton in C#

来源:互联网 发布:淘宝童装图片 编辑:程序博客网 时间:2024/06/05 01:09
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

//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){}
 }
};

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击