<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Java 單例 > 原文: [https://www.programiz.com/java-programming/singleton](https://www.programiz.com/java-programming/singleton) #### 在本教程中,我們將借助示例學習單例設計模式以及如何在 Java 中應用它。 單例是一種設計模式,而不是 Java 特有的功能。 它確保只創建一個類的實例。 設計模式就像我們的代碼庫一樣,其中包含世界各地程序員共享的各種編碼技術。 * * * ## Java 單例 這是我們如何在 Java 中使用單例。 * 創建`private`構造器,該構造器限制在類之外創建對象 * 創建一個引用單個對象的`private`屬性。 * 創建一個`public static`方法,該方法允許我們創建和訪問我們創建的對象。 在方法內部,我們將創建一個條件來限制我們創建多個對象。 這是一個例子。 ```java class SingletonExample { // private field that refers to the object private static SingletonExample singleObject; private SingletonExample() { // constructor of the SingletonExample class } public static SingletonExample getInstance() { // write code that allows us to create only one object // access the object as per our need } } ``` 在上面的示例中, * `private static SingletonExample singleObject` - 對類對象的引用。 * `private SingletonExample()` - 私有構造器,用于限制在類外部創建對象。 * `public static SingletonExample getInstance()` - 此方法返回對該類唯一對象的引用。 由于方法是`static`,因此可以使用類名進行訪問。 * * * ## 使用單例類 使用數據庫時可以使用單例。 它們可用于創建連接池以訪問數據庫,同時為所有客戶端重用相同的連接。 例如, ```java class Database { private static Database dbObject; private Database() { } public static Database getInstance() { // create object if it's not already created if(dbObject == null) { dbObject = new Database(); } // returns the singleton object return dbObject; } public void getConnection() { System.out.println("You are now connected to the database."); } } class Main { public static void main(String[] args) { Database db1; // refers to the only object of Database db1= Database.getInstance(); db1.getConnection(); } } ``` 當我們運行程序時,輸出將是: ```java You are now connected to the database. ``` 在上面的示例中, * 我們已經創建了一個單例類`Database`。 * `dbObject`是一個類類型字段。 這將引用類`Database`的對象。 * 私有構造器`Database()`防止在類外部創建對象。 * 靜態類類型方法`getInstance()`將類的實例返回到外部世界。 * 在`Main`類中,我們具有類類型變量`db1`。 我們正在使用`db1`調用`getInstance()`以獲取`Database`的唯一對象。 * 只能使用`Database`的對象訪問方法`getConnection()`。 * 由于`Database`只能有一個對象,因此所有客戶端都可以通過單個連接訪問數據庫。 * * * 重要的是要注意,只有少數情況(如記錄)使單例有意義。 甚至數據庫連接通常也不應是單例。 如果您不確定是否使用單例,建議您完全避免使用單例。 了解更多: 單例有什么不好?](https://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons "Why not to use singletons?")
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看