(DesignPattern)Singleton.-創(chuàng)新互聯(lián)

Role:(DesignPattern)S
ingleton.

The purpose of the Singleton pattern is to ensure that there is only one instance of a class, and that there is a global access point to that object.

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比肥鄉(xiāng)網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式肥鄉(xiāng)網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋肥鄉(xiāng)地區(qū)。費用合理售后完善,10余年實體公司更值得信賴。

Design:

  • Make the constructor private and add a private static constructor as well.
  • Add a private static read-only object that is internally instantialed using the private constructor.
  • Add a public static property that accesses the private object.

Implementation:

  public sealed class Singleton
    {
// Private constructor.   private Singleton()
        {
            Console.WriteLine("One instance is created."); 
        }

// private object instantiated with private constructor.   private static readonly Singleton instance = new Singleton(); 

// Public static property to get the object.   public static Singleton UniqueInstance
        {
get
            {
return instance; 
            }
        }
    }
  class Program
    {
static void Main(string[] args)
        {
            Singleton s0= Singleton.UniqueInstance;
            Singleton s1= Singleton.UniqueInstance;
            Singleton s2= Singleton.UniqueInstance; 
        }
    }

當前題目:(DesignPattern)Singleton.-創(chuàng)新互聯(lián)
本文鏈接:http://muchs.cn/article46/coephg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、品牌網(wǎng)站設(shè)計、自適應(yīng)網(wǎng)站、外貿(mào)建站、企業(yè)建站、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化