單例模式(singleton)

来源:http://www.cnblogs.com/webbdang/archive/2016/08/03/5732863.html
-Advertisement-
Play Games

1. 使用場景 來著stackorverflow controls concurrent access to a shared resource. access to the resource will be requested from multiple, disparate parts of t ...


1. 使用場景    

  來著stackorverflow  

  • controls concurrent access to a shared resource.
  • access to the resource will be requested from multiple, disparate parts of the system.
  • there can be only one object.
  • from (http://stackoverflow.com/questions/228164/on-design-patterns-when-to-use-the-singleton)

2. 簡單模式

public class BaseSingle
{
  public void Method()
  {
    Console.WriteLine("{1}.Method:{0}", this.GetHashCode(),this.GetType().ToString());
  }
}

public class UnsafeSingle : BaseSingle
{

private static UnsafeSingle _single;

public static UnsafeSingle UnsafeSingleInstatce()
{
  if (null == _single)
    _single = new UnsafeSingle();

  return _single;
}

private UnsafeSingle()
{
  Console.WriteLine(DateTime.Now.ToString() + ": 構造函數!");
}

}

缺點:對象未創建時,多線程同時訪問返回的對象並非一個;

測試代碼如下:

internal delegate void InstanceFun();

public void GetInstatnce()
{
  UnsafeSingle.UnsafeSingleInstatce().Method();
}

public void MutilThreadTest(InstanceFun instanceFun)
{
  var started = DateTime.Now;
  Thread[] threads = new Thread[100];

for (var index = 0; index < 100; index++)
{
  Thread thread = new Thread(new ThreadStart(instanceFun));
  threads[index] = thread;
}

  foreach (var thread in threads)
  {
    thread.Start();
  }
  Console.WriteLine("運行時長:{0};僅臨時測試", (DateTime.Now - started).Milliseconds);
}

  測試結果:  

DesignPattern.UnsafeSingle.Method:45338574
DesignPattern.UnsafeSingle.Method:30944461
DesignPattern.UnsafeSingle.Method:62452338
DesignPattern.UnsafeSingle.Method:43921088
DesignPattern.UnsafeSingle.Method:18573725
DesignPattern.UnsafeSingle.Method:8935080
DesignPattern.UnsafeSingle.Method:48823647
DesignPattern.UnsafeSingle.Method:28613802
DesignPattern.UnsafeSingle.Method:40642054
DesignPattern.UnsafeSingle.Method:52962918
DesignPattern.UnsafeSingle.Method:29095347
DesignPattern.UnsafeSingle.Method:62694277
DesignPattern.UnsafeSingle.Method:54643837
DesignPattern.UnsafeSingle.Method:36302867
DesignPattern.UnsafeSingle.Method:49913678
DesignPattern.UnsafeSingle.Method:52482287
DesignPattern.UnsafeSingle.Method:41829035
DesignPattern.UnsafeSingle.Method:57978397
DesignPattern.UnsafeSingle.Method:13456355
DesignPattern.UnsafeSingle.Method:23736788
DesignPattern.UnsafeSingle.Method:42195492
DesignPattern.UnsafeSingle.Method:28113429
DesignPattern.UnsafeSingle.Method:2875151
DesignPattern.UnsafeSingle.Method:56858914
DesignPattern.UnsafeSingle.Method:2735313
DesignPattern.UnsafeSingle.Method:50717245
DesignPattern.UnsafeSingle.Method:30697644
DesignPattern.UnsafeSingle.Method:47515266
DesignPattern.UnsafeSingle.Method:30183720
DesignPattern.UnsafeSingle.Method:65195544
DesignPattern.UnsafeSingle.Method:22695823
DesignPattern.UnsafeSingle.Method:8561069
DesignPattern.UnsafeSingle.Method:33135541
DesignPattern.UnsafeSingle.Method:17719913
DesignPattern.UnsafeSingle.Method:32493892
DesignPattern.UnsafeSingle.Method:57339132
DesignPattern.UnsafeSingle.Method:38516275
DesignPattern.UnsafeSingle.Method:33858004
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
運行時長:115
DesignPattern.UnsafeSingle.Method:58042889

 

 

後續更新


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 前言 Protocol Buffers (下麵簡稱PB)是一種輕便高效的結構化數據存儲格式,可以用於結構化數據串列化,很適合做數據存儲或 RPC 數據交換格式。它可用於通訊協議、數據存儲等領域的語言無關、平臺無關、可擴展的序列化結構數據格式。它支持多種語言,比如C++,Java,C ,Python, ...
  • 喜迎Win10周年版更新,湖南衛視旗下唯一官方視頻平臺《芒果TV》近日向Win10商店提交了芒果TV UWP V3.1.0版,這次不僅在上一版(V3.0.0)的基礎上完善了用戶呼聲最高的手機版視頻離線下載功能,而且為追劇達人帶來了跳過片頭片尾的貼心福利。 ...
  • 概述 XDT是Asp.net 4.0中的一個新特性,可以讓使用者在Web項目中在不同的生成類型下,快速切換配置文件(如在debug場景下使用測試配置資料庫,在Release場景下使用正式配置資料庫)。 但在非web項目中,VS並未提供如此方便的功能。這時如果我們同樣想使用xdt transforms ...
  • 今天編碼時遇到了一個問題,大致描述就是去讀取excel中的某列,如圖: 這些是文件名,類似一個對應關係表,然後代碼去查找對應的文件。 斷點調試出來的內容也是正常,但是都匹配不到(文件真實存在),在打出log時發現了端倪: 有些空格變成了問號。難道是我的打開方式不對麽。。。? 嘗試了幾種不同編碼打開, ...
  • 一、 通過AccountManagement 程式集(System.DirectoryServices.AccountManagement) acountManagement 包含有: 1. UserPrincipals 2. GroupPrincipal 3.ComputerPrincipals ...
  • 最近學習xamarin。剛好 手上有一個lumia 930.所以試一試把uwp app部署到手機上,並真機調試一把。 目前環境: 1.開發pc電腦是win10,版本1607.加入了insider,所以版本比較高。 2.手機是 lumia 930.版本 1511,手機未加入insider,所以是穩定版 ...
  • 放暑假了,生活基本穩定下來,項目的剩餘部分也要開始慢慢的研究了。 項目GitHub地址: https://github.com/Andyahui/xgyxsh_WeiXin SDK的GitHub地址:https://github.com/JeffreySu/WeiXinMPSDK/ SDK官方介紹博 ...
  • 一、前言 可能項目規模較小,項目中除了增刪改查就只剩下業務流程,以前都沒怎麼弄明白的東西時間長了就越發的模糊了... 二、使用場景 MSDN:delegate 是一種可用於封裝命名或匿名方法的引用類型。 委托類似於 C++ 中的函數指針;但是,委托是類型安全和可靠的。 BeginInvoke 方法啟 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...