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

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

相关推荐
特立独行的猫a2 小时前
HarmonyOS NEXT 诗词元服务项目开发上架全流程实战(一、项目介绍及实现效果)
华为·harmonyos·元服务·上架
云和数据.ChenGuang5 小时前
鸿蒙版电影app设计开发
华为·harmonyos·鸿蒙·鸿蒙系统
不当菜虚困5 小时前
JAVA设计模式——(八)单例模式
java·单例模式·设计模式
layneyao6 小时前
自动驾驶L4级技术落地:特斯拉、Waymo与华为的路线之争
人工智能·华为·自动驾驶
Bruce_Liuxiaowei6 小时前
HarmonyOS Next~鸿蒙系统UI创新实践:原生精致理念下的设计革命
ui·华为·harmonyos
成都被卷死的程序员7 小时前
单例模式介绍
单例模式
北漂老男孩9 小时前
设计模式全解析:23种经典设计模式及其应用
单例模式·设计模式
SuperHeroWu712 小时前
【HarmonyOS 5】鸿蒙检测系统完整性
华为·harmonyos·模拟器·系统完整性·越狱设备
京东云开发者12 小时前
Taro on Harmony :助力业务高效开发纯血鸿蒙应用
harmonyos
前端付豪13 小时前
2、ArkTS 是什么?鸿蒙最强开发语言语法全讲解(附实操案例)
前端·后端·harmonyos