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 pattern)的三种写法(含枚举实现方式)

最近项目组开始找人,试卷上豁然是个经典的问题(话说我毕业那会儿都已经是老题目,如今还在用)那就是, 哥们儿,写个单例模式吧! 恰巧今天早上看《effective java》看到单例模式的一种新写法。就...

单例模式-Singleton Pattern 确保对象的唯一性——单例模式 (四):一种更好的单例实现方法(静态内部类)

3.5 一种更好的单例实现方法        饿汉式单例类不能实现延迟加载,不管将来用不用始终占据内存;懒汉式单例类线程安全控制烦琐,而且性能受影响。可见,无论是饿汉式单例还是懒汉式单例都存在这...

Singleton pattern单例模式应用

  • 2012年01月13日 15:47
  • 82KB
  • 下载

单例模式(Singleton pattern)四种实现

单例实现1:经典单例模式(Classic singleton pattern) 实现延迟实例化(Lazy instantiaze);线程不安全(thread-unsafe)可用来学习单例模式...

单例模式(singleton pattern)的几种实现

第一种:使用静态成员实例。 #include using namespace std; class singleton { public: static singleton* GetInstan...

Python实现设计模式--01.单例模式(Singleton Pattern)

1、基于模块引用(饱汉模式) 建立文件Emperor_Singleton.py,内容如下: # 皇帝类 class Emperor(object): def say(self): ...

【设计模式】设计模式C++编程实现之单例模式(Singleton Pattern)

有一些对象我们只需要一个,比如说线程池(threadpool)缓存(cache)、对话框、处理偏好设置和注册表的对象、日志对象,充当打印机、显卡等设备的驱动程序的对象。如果制造出多个实例,则会导致许多...

Singleton Pattern单例模式

单例模式:确保一个类只有一个实例,并提供一个全局访问点。 类图 Singleton类,其构造方法(函数)是私有的,然后对外公开一个getInstance()方法来获取实例。 经典模式的Singleto...

Chromium源码浅析---单例模式(Singleton Pattern)在Chromium中的应用

单例模式是一种常见的软件设计模式,该模式设计的目的是只允许系统中出现一个该类的实例。在Chromium的多进程架构中,有且只有一个Browser进程,所有的Renderer进程都与Browser进程通...
  • yl02520
  • yl02520
  • 2013年12月07日 20:06
  • 1832
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Swift 实现单例模式Singleton pattern的三种方法
举报原因:
原因补充:

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