日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

Net设计模式实例之单例模式( Singleton Pattern)

發布時間:2024/4/14 asp.net 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Net设计模式实例之单例模式( Singleton Pattern) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、單例模式簡介(Brief Introduction

單例模式(Singleton Pattern),保證一個類只有一個實例,并提供一個訪問它的全局訪問點。單例模式因為Singleton封裝它的唯一實例,它就可以嚴格地控制客戶怎樣訪問它以及何時訪問它。

二、解決的問題(What To Solve

當一個類只允許創建一個實例時,可以考慮使用單例模式。??????

三、單例模式分析(Analysis

1、單例模式結構

?

?

Singleton,定義一個私有變量instance;私有構造方法Singleton()和方法GetInstance();

私有變量instance:

private static Singleton instance;

私有構造方法Singleton(),外界不能使用new關鍵字來創建此類的實例了。

private Singleton()

{

}

方法GetInstance(),?此方法是本類實例的唯一全局訪問點。

public static Singleton GetInstance()

{

????//如實例不存在,則New一個新實例,否則返回已有實例

????if (instance == null)

????{

????????instance = new Singleton();

????}

????return instance;

}

2、代碼

1、單例模式類Singleton

public?class?Singleton

{

????private?static?Singleton?instance;

?

????///?<summary>

????///?程序運行時,創建一個靜態只讀的進程輔助對象

????///?</summary>

????private?static?readonly?object?_object =?new?object();

?

????///?<summary>

????///?構造方法私有,外鍵不能通過New類實例化此類

????///?</summary>

????private?Singleton()

????{

????}

????///?<summary>

????///?此方法是本類實例的唯一全局訪問點

????///?(雙重加鎖?Double-Check Locking)

????///?</summary>

????///?<returns></returns>

????public?static?Singleton?GetInstance()

????{

????????//先判斷實例是否存在,不存在再加鎖處理

????????if?(instance ==?null)

????????{

????????????//在同一時刻加了鎖的那部分程序只有一個線程可以進入,

????????????lock?(_object)

????????????{

????????????????//如實例不存在,則New一個新實例,否則返回已有實例

????????????????if?(instance ==?null)

????????????????{

????????????????????instance =?new?Singleton();

????????????????}

????????????}

????????}

????????return?instance;

????}

}

?

2、客戶端代碼

static?void?Main(string[] args)

{

????Singleton?singleton1 =?Singleton.GetInstance();

????Singleton?singleton2 =?Singleton.GetInstance();

????if?(singleton1 ==singleton2)

????{

????????Console.WriteLine("實例singleton1與實例singleton2相同!");

????}

????Console.ReadKey();

}

3、實例運行結果

?

?

四.實例分析(Example

1、場景

Mail發送機制中,需要對已經發送的消息做Log。同一時間內只允許一個進程對Txt文檔進行操作,此時使用單例模式比較合適。結構如下圖所示

?

?

WriteMailLog(string message)?方法:紀錄Mail發送日志到文件.

_helper?、_fileLock:程序運行時,創建2個靜態只讀的進程輔助對象

2、代碼

1、類MailLog

public?class?EmailLog

{

????private?static?object?_helper =?new?object();

????private?static?EmailLog?_instance;

????private?static?object?_fileLock =?new?object();

?

????private?EmailLog()

????{}

?

????public?static?EmailLog?GetInstance()

????{

????????lock?(_helper)

????????{

????????????if?(_instance ==?null)

????????????{

????????????????_instance =?new?EmailLog();

????????????}

????????}

????????return?_instance;

????}

?

????///?<summary>

????///?發送Mail日志

????///?</summary>

????///?<param name="message">信息</param>

????public?void?WriteEmailLog(string?message)

????{

????????string?filePath = System.AppDomain.CurrentDomain.BaseDirectory +"mail.txt";

????????StreamWriter?sw =?null;

????????FileStream?fs =?null;

????????lock?(_fileLock)

????????{

????????????if?(!File.Exists(filePath))

????????????{

????????????????fs = System.IO.File.Create(filePath);

????????????????sw =?new?StreamWriter(fs,?Encoding.UTF8);

????????????????sw.WriteLine("--------------------------------------------------------------------------");

????????????????sw.WriteLine(message);

????????????????sw.Flush();

????????????????sw.Close();

????????????}

????????????else

????????????{

????????????????fs =?new?FileStream(filePath,?FileMode.Append);

????????????????sw =?new?StreamWriter(fs,?Encoding.UTF8);

????????????????sw.WriteLine("--------------------------------------------------------------------------");

????????????????sw.WriteLine(message);

????????????????sw.Flush();

????????????????sw.Close();

????????????}

????????}

????}

}

?

2、客戶端代碼

static?void?Main(string[] args)

{

????EmailLog?w1 =?EmailLog.GetInstance();

????w1.WriteEmailLog("發送Mail給靈動生活...");

????EmailLog?w2 =?EmailLog.GetInstance();

????w2.WriteEmailLog("發送Mail給James Hao...");

}

3、實例運行結果

?

五、總結(Summary

本文對單例模式(Singleton Pattern)的概念及其設計結構圖簡單地進行了描述,同樣也以一個Mail機制的LOG實例進行了說明。單例模式是比較常用。比較簡單的設計模式。

?

轉載自:http://www.cnblogs.com/ywqu/archive/2010/01/13/1646009.html

?

轉載于:https://www.cnblogs.com/Wolfmanlq/p/6036047.html

總結

以上是生活随笔為你收集整理的Net设计模式实例之单例模式( Singleton Pattern)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。