<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庫中使用,但是這有個更直接的示例: ```java // patterns/SingletonPattern.java interface Resource { int getValue(); void setValue(int x); } /* * 由于這不是從Cloneable基類繼承而且沒有添加可克隆性, * 因此將其設置為final可防止通過繼承添加可克隆性。 * 這也實現了線程安全的延遲初始化: */ final class Singleton { private static final class ResourceImpl implements Resource { private int i; private ResourceImpl(int i) { this.i = i; } public synchronized int getValue() { return i; } public synchronized void setValue(int x) { i = x; } } private static class ResourceHolder { private static Resource resource = new ResourceImpl(47); } public static Resource getResource() { return ResourceHolder.resource; } } public class SingletonPattern { public static void main(String[] args) { Resource r = Singleton.getResource(); System.out.println(r.getValue()); Resource s2 = Singleton.getResource(); s2.setValue(9); System.out.println(r.getValue()); try { // 不能這么做,會發生:compile-time error(編譯時錯誤). // Singleton s3 = (Singleton)s2.clone(); } catch(Exception e) { throw new RuntimeException(e); } } } /* Output: 47 9 */ ``` 創建單例的關鍵是防止客戶端程序員直接創建對象。 在這里,這是通過在Singleton類中將Resource的實現作為私有類來實現的。 此時,你將決定如何創建對象。在這里,它是按需創建的,在第一次訪問的時候創建。 該對象是私有的,只能通過public getResource()方法訪問。 懶惰地創建對象的原因是它嵌套的私有類resourceHolder在首次引用之前不會加載(在getResource()中)。當Resource對象加載的時候,靜態初始化塊將被調用。由于JVM的工作方式,這種靜態初始化是線程安全的。為保證線程安全,Resource中的getter和setter是同步的。
                  <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>

                              哎呀哎呀视频在线观看