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

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

相关推荐
Georgewu5 小时前
【HarmonyOS】元服务入门详解 (一)
harmonyos
coder_pig6 小时前
跟🤡杰哥一起学Flutter (三十五、玩转Flutter滑动机制📱)
android·flutter·harmonyos
络78 小时前
Java4种设计模式详解(单例模式、工厂模式、适配器模式、代理模式)
单例模式·设计模式·代理模式·适配器模式·工厂模式
睿麒10 小时前
鸿蒙app 开发中的Record<string,string>的用法和含义
华为·harmonyos
Fanmeang13 小时前
OSPF与BGP的联动特性实验案例
运维·网络·华为·ospf·bgp·路由黑洞·ospf联动bgp
cainiao08060513 小时前
华为HarmonyOS 5.0深度解析:跨设备算力池技术白皮书(2025全场景智慧中枢)
华为·harmonyos
小刘|13 小时前
单例模式详解
java·开发语言·单例模式
万少13 小时前
04-自然壁纸实战教程-搭建基本工程
前端·harmonyos·客户端
yrjw15 小时前
FileSaver是一个为HarmonyOS ArkTS应用设计的开源库,提供便捷的文件保存功能。主要特性包括:支持将图片保存至系统相册和应用沙盒存储,支持多种
harmonyos
xo1988201116 小时前
鸿蒙选择本地视频文件,并获取首帧预览图
华为·harmonyos