Swift 实现单例模式Singleton pattern的三种方法

转载 2015年11月20日 10:07:49


From my short experience with Swift there are three approaches to implement the Singleton pattern that support lazy initialization and thread safety.

These approaches might change or become redundant as the language matures.

Global constant

<code style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline; white-space: inherit;"><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">let</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">_SingletonSharedInstance</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">=</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Singleton</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">()</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">

</span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">class</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Singleton</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">  </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">{</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
    </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">class</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">var</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> sharedInstance </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">:</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Singleton</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">{</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
        </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">return</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">_SingletonSharedInstance</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
    </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">}</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">}</span></span></code>

We use a global constant because class constants are not yet supported.

This approach supports lazy initialization because Swift lazily initializes global constants (and variables), and is thread safe by virtue of let.

Nested struct

<code style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline; white-space: inherit;"><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">class</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Singleton</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">{</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
    </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">class</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">var</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> sharedInstance </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">:</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Singleton</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">{</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
        </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">struct</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Static</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">{</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
            </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">static</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">let</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> instance </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">:</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Singleton</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">=</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Singleton</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">()</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
        </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">}</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
        </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">return</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Static</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">.</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">instance
    </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">}</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">}</span></span></code>

Unlike classes, structs do support static constants. By using a nested struct we can leverage its static constant as a class constant.

dispatch_once

The traditional Objective-C approach ported to Swift. I'd say it's no longer necessary to use this approach but I'm putting it here anyway as I find the differences in syntax interesting.

<code style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; vertical-align: baseline; white-space: inherit;"><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">class</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Singleton</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">{</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
    </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">class</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">var</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> sharedInstance </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">:</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Singleton</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">{</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
        </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">struct</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Static</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">{</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
            </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">static</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">var</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> onceToken </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">:</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">dispatch_once_t</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">=</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="lit" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(128, 0, 0); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">0</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
            </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">static</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">var</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> instance </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">:</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Singleton</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">?</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">=</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">nil</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
        </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">}</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
        dispatch_once</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">(&</span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Static</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">.</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">onceToken</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">)</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">{</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
            </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Static</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">.</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">instance </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">=</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Singleton</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">()</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
        </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">}</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
        </span></span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(0, 0, 139); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">return</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);"> </span></span><span class="typ" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: rgb(43, 145, 175); vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">Static</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">.</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">instance</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">!</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
    </span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">}</span></span><span class="pln" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">
</span></span><span class="pun" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; vertical-align: baseline; background-color: transparent;"><span style="background-color: rgb(238, 238, 238);">}</span></span></code>

设计模式之---单例模式(Singleton Design Pattern)

场景 对象只要利用自己的属性完成了自己的任务.那该对象就是承担了责任。 除了维持了自身的一致性,该对象无需承担其他任何责任。 如果该对象还承担着其他责任,而其他对象又依赖于该特定对象所承担的贵任...
  • u010878994
  • u010878994
  • 2016年04月26日 01:19
  • 585

设计模式(创建型)之单例模式(Singleton Pattern)

单例模式可能是23种设计模式中最简单的。应用也非常广泛,譬如Android中的数据库访问等操作都可以运用单例模式。核心概念: 确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例,这个类称...
  • yanbober
  • yanbober
  • 2015年04月27日 16:50
  • 2680

设计模式——单例模式(Singleton Pattern)实现

什么是单例模式? 单例模式(Singleton Parttern)保证一个类仅有一个实例,并提供一个访问它的全局访问点。类似于全局变量,但是效果比全局变量更优,全局变量必须在程序开始就创建,如果后面没...
  • a987073381
  • a987073381
  • 2016年08月07日 23:01
  • 421

Java单例模式的三种实现方式

1, singletonpublic class Singleton { private Singleton() { System.out.println("This is co...
  • gsw204
  • gsw204
  • 2016年09月06日 19:37
  • 983

Swift 实现单例模式Singleton pattern的三种方法

From my short experience with Swift there are three approaches to implement the Singleton pattern th...
  • songrotek
  • songrotek
  • 2014年06月18日 16:18
  • 11590

singleton模式四种线程安全的实现

1.描述:     Singleton(单例)是设计模式的一种,为了保证一个类仅有一个实例,并提供一个访问它的全局访问点。 2.主要特点:          1)单例类确保自己只有一个实例(...
  • u013256816
  • u013256816
  • 2015年12月29日 15:30
  • 10942

单例模式的五种实现方式

单例模式可以说是23中设计模式中应用最广的一种模式了。        定义:确保某一个类只有一个实例,自行实例化并且想整个系统提供这个实例。       使用场景:避免某个类产生多个对象而消耗过多...
  • soul_code
  • soul_code
  • 2015年12月05日 09:53
  • 8656

单例模式的6种实现方式

为什么使用单例模式需要确保某个类只要一个对象,或创建一个类需要消耗的资源过多,如访问IO和数据库操作等,这时就需要考虑使用单例模式了。使用单例模式需要注意的关键点 将构造函数访问修饰符设置为priva...
  • ts1900
  • ts1900
  • 2016年05月13日 16:03
  • 1650

java实现单例singleton的几种模式

Singleton的4种实现方式1.静态工厂实现法静态工场估计是最常用的单例实现方式,例子如下(详细见注解)// 静态工厂实现法 public class CommonSingleton { /...
  • u012233832
  • u012233832
  • 2016年08月08日 23:37
  • 393

C#设计模式——单例模式(Singleton)

一、引言 最近在设计模式的一些内容,主要的参考书籍是《Head First 设计模式》,同时在学习过程中也查看了很多博客园中关于设计模式的一些文章的,在这里记录下我的一些学习笔记,一是为了帮助我更深...
  • ycl295644
  • ycl295644
  • 2015年07月06日 08:53
  • 2131
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Swift 实现单例模式Singleton pattern的三种方法
举报原因:
原因补充:

(最多只允许输入30个字)