游戏编程模式06-设计模式-单例模式

设计模式-单例模式

参考章节:https://gpp.tkchu.me/singleton.html

脑内画面

单例模式保证某个类在进程里只有一个实例,并提供一个全局入口。它像游戏里的总控开关:大家都能摸到它,但也正因为大家都能摸到,它很容易变成隐形依赖的集合点。
#mermaid-svg-6ech9FBI6JuohGlW{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-6ech9FBI6JuohGlW .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-6ech9FBI6JuohGlW .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-6ech9FBI6JuohGlW .error-icon{fill:#552222;}#mermaid-svg-6ech9FBI6JuohGlW .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-6ech9FBI6JuohGlW .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-6ech9FBI6JuohGlW .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-6ech9FBI6JuohGlW .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-6ech9FBI6JuohGlW .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-6ech9FBI6JuohGlW .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-6ech9FBI6JuohGlW .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-6ech9FBI6JuohGlW .marker{fill:#333333;stroke:#333333;}#mermaid-svg-6ech9FBI6JuohGlW .marker.cross{stroke:#333333;}#mermaid-svg-6ech9FBI6JuohGlW svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-6ech9FBI6JuohGlW p{margin:0;}#mermaid-svg-6ech9FBI6JuohGlW .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-6ech9FBI6JuohGlW .cluster-label text{fill:#333;}#mermaid-svg-6ech9FBI6JuohGlW .cluster-label span{color:#333;}#mermaid-svg-6ech9FBI6JuohGlW .cluster-label span p{background-color:transparent;}#mermaid-svg-6ech9FBI6JuohGlW .label text,#mermaid-svg-6ech9FBI6JuohGlW span{fill:#333;color:#333;}#mermaid-svg-6ech9FBI6JuohGlW .node rect,#mermaid-svg-6ech9FBI6JuohGlW .node circle,#mermaid-svg-6ech9FBI6JuohGlW .node ellipse,#mermaid-svg-6ech9FBI6JuohGlW .node polygon,#mermaid-svg-6ech9FBI6JuohGlW .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-6ech9FBI6JuohGlW .rough-node .label text,#mermaid-svg-6ech9FBI6JuohGlW .node .label text,#mermaid-svg-6ech9FBI6JuohGlW .image-shape .label,#mermaid-svg-6ech9FBI6JuohGlW .icon-shape .label{text-anchor:middle;}#mermaid-svg-6ech9FBI6JuohGlW .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-6ech9FBI6JuohGlW .rough-node .label,#mermaid-svg-6ech9FBI6JuohGlW .node .label,#mermaid-svg-6ech9FBI6JuohGlW .image-shape .label,#mermaid-svg-6ech9FBI6JuohGlW .icon-shape .label{text-align:center;}#mermaid-svg-6ech9FBI6JuohGlW .node.clickable{cursor:pointer;}#mermaid-svg-6ech9FBI6JuohGlW .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-6ech9FBI6JuohGlW .arrowheadPath{fill:#333333;}#mermaid-svg-6ech9FBI6JuohGlW .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-6ech9FBI6JuohGlW .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-6ech9FBI6JuohGlW .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-6ech9FBI6JuohGlW .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-6ech9FBI6JuohGlW .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-6ech9FBI6JuohGlW .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-6ech9FBI6JuohGlW .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-6ech9FBI6JuohGlW .cluster text{fill:#333;}#mermaid-svg-6ech9FBI6JuohGlW .cluster span{color:#333;}#mermaid-svg-6ech9FBI6JuohGlW div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-6ech9FBI6JuohGlW .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-6ech9FBI6JuohGlW rect.text{fill:none;stroke-width:0;}#mermaid-svg-6ech9FBI6JuohGlW .icon-shape,#mermaid-svg-6ech9FBI6JuohGlW .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-6ech9FBI6JuohGlW .icon-shape p,#mermaid-svg-6ech9FBI6JuohGlW .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-6ech9FBI6JuohGlW .icon-shape .label rect,#mermaid-svg-6ech9FBI6JuohGlW .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-6ech9FBI6JuohGlW .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-6ech9FBI6JuohGlW .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-6ech9FBI6JuohGlW :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 玩法代码
SaveSystem.Instance
UI
调试工具
存档文件

它解决的问题

有些服务在技术上确实只应存在一个,例如平台成就接口、存档管理、硬件音频设备包装层。单例提供唯一实例和延迟初始化。但在游戏架构里,单例最常见的问题是:它让依赖关系从构造函数里消失,藏进任何地方都能访问的静态属性。

C# 示例

csharp 复制代码
public sealed class SaveSystem
{
    public static SaveSystem Instance { get; } = new SaveSystem();

    private readonly Dictionary<string, string> _values = new();

    private SaveSystem()
    {
    }

    public void SetString(string key, string value)
    {
        _values[key] = value;
    }

    public string? GetString(string key)
    {
        return _values.TryGetValue(key, out var value) ? value : null;
    }
}

public sealed class SettingsMenu
{
    public void ChangeLanguage(string language)
    {
        SaveSystem.Instance.SetString("language", language);
    }
}

更可控的替代写法

csharp 复制代码
public interface ISaveSystem
{
    void SetString(string key, string value);
    string? GetString(string key);
}

public sealed class SettingsMenu2
{
    private readonly ISaveSystem _saveSystem;

    public SettingsMenu2(ISaveSystem saveSystem)
    {
        _saveSystem = saveSystem;
    }

    public void ChangeLanguage(string language)
    {
        _saveSystem.SetString("language", language);
    }
}

什么时候用

  • 对象天然唯一,并且重复实例会造成错误。
  • 初始化顺序清晰,生命周期贯穿整个游戏。
  • 测试不需要频繁替换该对象,或者已经提供替换通道。

使用时的锋利边

单例常把两个概念绑在一起:唯一实例和全局访问。真正危险的是全局访问。它会让代码表面上没有依赖,实际上依赖遍布各处。大型项目里更推荐依赖注入、服务定位器或显式上下文对象。