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

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

相关推荐
coder_pig7 小时前
跟🤡杰哥一起学Flutter (三十四、玩转Flutter手势✋)
前端·flutter·harmonyos
simple丶7 小时前
【HarmonyOS】鸿蒙蓝牙连接与通信技术
harmonyos·arkts·arkui
二二孚日8 小时前
自用华为ICT云赛道Big Data第五章知识点-Flume海量日志聚合
大数据·华为
前端世界9 小时前
HarmonyOS开发实战:鸿蒙分布式生态构建与多设备协同发布全流程详解
分布式·华为·harmonyos
Jalor10 小时前
Flutter + 鸿蒙 | Flutter 跳转鸿蒙原生界面
flutter·harmonyos
二二孚日10 小时前
自用华为ICT云赛道Big Data第四章知识点-Flink流批一体分布式实时处理引擎
大数据·华为
zhanshuo11 小时前
开发者必看!如何在HarmonyOS中快速调用摄像头功能
harmonyos
HMSCore11 小时前
借助HarmonyOS SDK,《NBA巅峰对决》实现“分钟级启动”到“秒级进场”
harmonyos
zhanshuo11 小时前
鸿蒙UI开发全解:JS与Java双引擎实战指南
前端·javascript·harmonyos
HarmonyOS小助手11 小时前
闯入鸿蒙:浪漫、理想与「草台班子」
harmonyos·鸿蒙·harmonyos next·鸿蒙生态