鸿蒙 Next 实现单例

鸿蒙 Next 实现单例

在鸿蒙 Next 开发中,单例模式是一种常用的设计模式,用于确保一个类只有一个实例,并提供一个全局访问点。本文将详细介绍如何在鸿蒙 Next 中实现单例模式,并提供几种常见的实现方式。

一、单例模式的实现方式

(一)基本写法

这是最简单的单例实现方式,通过静态变量和私有构造函数来确保只有一个实例。

typescript 复制代码
class Singleton {
  private static instance: Singleton | null = null;

  private constructor() {}

  public static getInstance(): Singleton {
    if (!Singleton.instance) {
      Singleton.instance = new Singleton();
    }
    return Singleton.instance;
  }
}

(二)饿汉式单例

饿汉式单例在类加载时就创建实例,适用于实例创建成本较低的场景。

typescript 复制代码
export class Singleton {
  private static instance = new Singleton();

  private constructor() {}

  public static getInstance(): Singleton {
    return Singleton.instance;
  }
}

(三)懒汉式单例

懒汉式单例在第一次调用 getInstance 时创建实例,适用于实例创建成本较高的场景。

typescript 复制代码
class Singleton {
  private static instance: Singleton | null = null;

  private constructor() {}

  public static getInstance(): Singleton {
    if (!Singleton.instance) {
      Singleton.instance = new Singleton();
    }
    return Singleton.instance;
  }
}

(四)双重检查锁定

双重检查锁定是一种线程安全的单例实现方式,适用于多线程环境。

typescript 复制代码
export class Singleton {
  private static instance: Singleton;

  private constructor() {}

  public static getInstance(): Singleton {
    if (!Singleton.instance) {
      synchronized(Singleton) {
        if (!Singleton.instance) {
          Singleton.instance = new Singleton();
        }
      }
    }
    return Singleton.instance;
  }
}

(五)静态内部类单例

静态内部类单例利用了类加载机制来保证线程安全。

typescript 复制代码
export class Singleton {
  private constructor() {}

  private static class SingletonHolder {
    static readonly instance = new Singleton();
  }

  public static getInstance(): Singleton {
    return SingletonHolder.instance;
  }
}

二、多线程环境下的单例

在多线程环境下,确保单例的线程安全性至关重要。可以使用双重检查锁定或静态内部类来实现。

(一)双重检查锁定

typescript 复制代码
export class Singleton {
  private static instance: Singleton;

  private constructor() {}

  public static getInstance(): Singleton {
    if (!Singleton.instance) {
      synchronized(Singleton) {
        if (!Singleton.instance) {
          Singleton.instance = new Singleton();
        }
      }
    }
    return Singleton.instance;
  }
}

(二)静态内部类单例

typescript 复制代码
export class Singleton {
  private constructor() {}

  private static class SingletonHolder {
    static readonly instance = new Singleton();
  }

  public static getInstance(): Singleton {
    return SingletonHolder.instance;
  }
}

三、跨模块共享单例

在鸿蒙 Next 中,跨模块共享单例需要确保不同模块访问到的是同一个实例。可以通过将单例放在 HSP 包中,让其他模块引用。

typescript 复制代码
// HSP 包中的单例
class Singleton {
  private static instance: Singleton | null = null;

  private constructor() {}

  public static getInstance(): Singleton {
    if (!Singleton.instance) {
      Singleton.instance = new Singleton();
    }
    return Singleton.instance;
  }
}

然后在其他模块中引用这个单例:

typescript 复制代码
import { Singleton } from 'path/to/hsp/package';

const singletonInstance = Singleton.getInstance();

四、总结

单例模式在鸿蒙 Next 开发中非常实用,尤其是在管理全局状态、配置信息和资源访问时。通过上述几种实现方式,开发者可以根据具体需求选择合适的单例模式。在多线程环境下,确保线程安全是关键,而跨模块共享单例则需要合理组织代码结构。

希望本文能帮助你更好地理解和实现单例模式。如果有任何问题或需要进一步讨论,欢迎随时交流!

相关推荐
●VON7 分钟前
Flutter组件深度解析:从基础到高级的完整指南
android·javascript·flutter·harmonyos·von
讯方洋哥20 分钟前
HarmonyOS App开发——鸿蒙ArkTS的ibestUI在鸿蒙PC集成和应用
harmonyos
国医中兴21 分钟前
ClickHouse集群部署与管理:从0到1的实战指南
flutter·harmonyos·鸿蒙·openharmony
想你依然心痛1 小时前
HarmonyOS 5.0游戏开发实战:基于ArkGraphics 3D的轻量级物理引擎游戏
游戏·3d·harmonyos
木斯佳2 小时前
HarmonyOS 6 SDK对接实战:从原生ASR到Copilot SDK(上)- 手写语音识别实现
copilot·语音识别·harmonyos
脑极体4 小时前
逆风的方向,华为Mate在飞翔
华为
讯方洋哥4 小时前
HarmonyOS App开发——鸿蒙ArkTS端云一体化的云函数实现机制
harmonyos
木斯佳17 小时前
HarmonyOS 6 三方SDK对接:从半接模式看Share Kit原理——系统分享的运行机制与设计理念
设计模式·harmonyos·架构设计·分享·半接模式
被温水煮的青蛙18 小时前
HarmonyOS openCustomDialog 实战:从入门到理解原理
harmonyos
高一学习c++会秃头吗18 小时前
鸿蒙适应式布局和响应式布局零基础
harmonyos