首页 > 其他 > 详细

乐在其中设计模式(C#) - 单例模式(Singleton Pattern)

时间:2014-03-09 21:35:26      阅读:422      评论:0      收藏:0      [点我收藏+]
原文:乐在其中设计模式(C#) - 单例模式(Singleton Pattern)

[索引页]
[源码下载]


乐在其中设计模式(C#) - 单例模式(Singleton Pattern)


作者:webabcd


介绍
保证一个类仅有一个实例,并提供一个访问它的全局访问点。


示例
保证一个类仅有一个实例。
bubuko.com,布布扣


Singleton
bubuko.com,布布扣using System;
bubuko.com,布布扣
using System.Collections.Generic;
bubuko.com,布布扣
using System.Text;
bubuko.com,布布扣
bubuko.com,布布扣
namespace Pattern.Singleton
bubuko.com,布布扣
{
bubuko.com,布布扣    
/// <summary>
bubuko.com,布布扣    
/// 泛型实现单例模式
bubuko.com,布布扣    
/// </summary>
bubuko.com,布布扣    
/// <typeparam name="T">需要实现单例的类</typeparam>

bubuko.com,布布扣    public class Singleton<T> where T : new()
bubuko.com,布布扣    
{
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 返回类的实例
bubuko.com,布布扣        
/// </summary>

bubuko.com,布布扣        public static T Instance
bubuko.com,布布扣        
{
bubuko.com,布布扣            
get return SingletonCreator.instance; }
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
class SingletonCreator
bubuko.com,布布扣        
{
bubuko.com,布布扣            
internal static readonly T instance = new T();
bubuko.com,布布扣        }

bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣


Test
bubuko.com,布布扣using System;
bubuko.com,布布扣
using System.Data;
bubuko.com,布布扣
using System.Configuration;
bubuko.com,布布扣
using System.Collections;
bubuko.com,布布扣
using System.Web;
bubuko.com,布布扣
using System.Web.Security;
bubuko.com,布布扣
using System.Web.UI;
bubuko.com,布布扣
using System.Web.UI.WebControls;
bubuko.com,布布扣
using System.Web.UI.WebControls.WebParts;
bubuko.com,布布扣
using System.Web.UI.HtmlControls;
bubuko.com,布布扣
bubuko.com,布布扣
using Pattern.Singleton;
bubuko.com,布布扣
bubuko.com,布布扣
public partial class Singleton : System.Web.UI.Page
bubuko.com,布布扣
{
bubuko.com,布布扣    
protected void Page_Load(object sender, EventArgs e)
bubuko.com,布布扣    
{
bubuko.com,布布扣        
// 使用单例模式,保证一个类仅有一个实例
bubuko.com,布布扣
        Response.Write(Singleton<Test>.Instance.Time);
bubuko.com,布布扣        Response.Write(
"<br />");
bubuko.com,布布扣        Response.Write(Singleton
<Test>.Instance.Time);
bubuko.com,布布扣        Response.Write(
"<br />");
bubuko.com,布布扣
bubuko.com,布布扣        
// 不用单例模式
bubuko.com,布布扣
        Test t = new Test();
bubuko.com,布布扣        Response.Write(t.Time);
bubuko.com,布布扣        Response.Write(
"<br />");
bubuko.com,布布扣        Test t2 
= new Test();
bubuko.com,布布扣        Response.Write(t2.Time);
bubuko.com,布布扣        Response.Write(
"<br />");
bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣
bubuko.com,布布扣
public class Test
bubuko.com,布布扣
{
bubuko.com,布布扣    
private DateTime _time;
bubuko.com,布布扣
bubuko.com,布布扣    
public Test()
bubuko.com,布布扣    
{
bubuko.com,布布扣        System.Threading.Thread.Sleep(
3000);
bubuko.com,布布扣        _time 
= DateTime.Now;    
bubuko.com,布布扣    }

bubuko.com,布布扣
bubuko.com,布布扣    
public string Time
bubuko.com,布布扣    
{
bubuko.com,布布扣        
get return _time.ToString(); }
bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣

运行结果
2007-2-10 22:35:11
2007-2-10 22:35:11
2007-2-10 22:35:14
2007-2-10 22:35:17


参考
http://www.dofactory.com/Patterns/PatternSingleton.aspx



OK
[源码下载] 

乐在其中设计模式(C#) - 单例模式(Singleton Pattern),布布扣,bubuko.com

乐在其中设计模式(C#) - 单例模式(Singleton Pattern)

原文:http://www.cnblogs.com/lonelyxmas/p/3590332.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!