国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Singleton設計模式 - 創造性設計模式

2019-11-09 14:49:31
字體:
來源:轉載
供稿:網友

顧名思義,Singleton意味著“單實例”。因為它是一個創造性的設計模式,所以這種設計模式提供了解決方案,在整個應用程序的生命周期只創建一個類的一個對象(實例)。這意味著您可以通過提供一個靜態成員來限制給定類的對象的創建函數僅為該類創建一個對象。一旦對象第一次創建,那么如果你將嘗試為同一個類創建另一個對象,那么它將返回現有對象的引用(活對象的引用)給你。這樣,它可以限制到特定類的一個對象。要僅限制類的一個對象創建,您必須按照以下步驟。創建Singleton類的步驟創建類(可以使用C ++ / java / .Net編程語言創建類)使默認構造函數為PRivate,以便沒有人可以使用Operator new / calloc / malloc operator等創建任何對象。使用相同類型的私有變量來跟蹤對象是否已經被創建,只需檢查變量的空值,如果它不為null,那么只返回該對象的現有引用。如果對象沒有被創建,那么只使用普通的new操作符首次創建類的一個對象,然后返回創建的對象的引用。定義一個靜態成員函數,可以被其他類訪問以調用創建此類的對象。

單例設計模式(Singleton Class)

看看例子,并閱讀代碼行的注釋更清楚。這是java代碼,你可以使用C ++和.net編程使用相同的過程。

123456789101112131415161718192021222324252627282930313233public class SingletonDemo{        //Make the constructor private, so no other call can create object of           //this class directly using new operator.        private SingletonDemo (){}  /*Create a private member variable of same class type(SingletonDemo Class here),    so that we can store the single object value in this variable    and maintain it throughout the application life time*/  private static SingletonDemo objSingletonDemo;     /*Create a static method to access from other classes which returns the existing objects reference. Check the null values of the object as below code, and if it is null then create the object for the first time only and return it. If it is not null, then just return the previous value.*/     public static SingletonDemo getInstance() {      if(null == objSingletonDemo)   {        objSingletonDemo = new SingletonDemo();      }      return objSingletonDemo;    }     public void testFun() {      // do something here       System.out.println("Hello SingletonDemo....");    }}

現在是時候測試上面的Singleton類代碼。

1234567891011  // Now it is time to use the above singleton class    publicstaticvoidmain(Stringa[])    {      // Create an object of the SingletonDemo class by calling getInstance()      //static function of that class and use it's functionality.         SingletonDemoobjSingleTone1=SingletonDemo.getInstance();        objSingleTone1.testFun();     //Note: If you will call like below, then it will give error message.    // SingletonDemo objSingletonDemo = new SingletonDemo();  }

使用Singleton設計模式此設計模式通常用于以下應用程序類型。它保存網絡特定數據,如果一個服務器將在特定時間停機,那么它也可以同時管理您的請求與其他服務器。如果要將當前數據或對象的更新共享給所有其他模塊,則此單例設計模式是最好的方法。您還可以使用下面提到的單例模式開發不同類型的應用程序。銀行應用程序財務應用旅游應用系統服務類

關于單例設計模式的面試問題

我想分享我的實時面試經驗,有99%的機會,你會在你的電話采訪或面對面采訪中面對Singleton類的問題。所以準備好答案。在Singleton模式或Singleton類中找到一些常見問題。1.如何創建一個Singleton類。答:請檢查上面的代碼(SingletoneDemo類),以供參考。2.如何限制應用程序使用new運算符創建該類的對象。答案:通過將構造函數聲明為private并提供一個靜態方法來創建該類的新對象。請檢查上面的類(SingletoneDemo類)以供參考。3.如何在整個應用程序中只處理類的一個實例。答案:通過使用Singleton類。請檢查上面的類(SingletoneDemo類)以供參考。


上一篇:百度定位sdk接入

下一篇:CIFilter濾鏡

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 开封县| 日土县| 仁寿县| 寿宁县| 临城县| 闽侯县| 绵竹市| 东山县| 伊宁县| 阳信县| 沁水县| 莎车县| 临武县| 金川县| 大厂| 磴口县| 平塘县| 西乌| 政和县| 铁岭县| 公安县| 江阴市| 拜泉县| 民和| 宜良县| 青冈县| 清水县| 小金县| 黄浦区| 曲麻莱县| 平乐县| 承德市| 桂平市| 年辖:市辖区| 延边| 石家庄市| 新和县| 邛崃市| 普格县| 上蔡县| 右玉县|