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

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

相关推荐
别说我什么都不会1 小时前
OpenHarmony源码分析之分布式软总线:trans_service模块(5)/TCP会话管理
分布式·嵌入式·harmonyos
二流小码农1 小时前
鸿蒙开发:ArkTs语言注释
android·ios·harmonyos
二流小码农2 小时前
鸿蒙开发:权限授权封装
android·ios·harmonyos
H.ZWei3 小时前
鸿蒙应用开发—ZDbUtil高效使用数据库
数据库·harmonyos·鸿蒙·zdbutil
儿歌八万首3 小时前
HarmonyOS Next 中的自定义弹出框 用法
harmonyos
lqj_本人4 小时前
鸿蒙 @ohos.arkui.drawableDescriptor (DrawableDescriptor)
华为·harmonyos
lqj_本人4 小时前
鸿蒙 @ohos.arkui.componentUtils (componentUtils)
华为·harmonyos
看客随心4 小时前
鸿蒙路由 HMRouter 配置及使用 三 全局拦截器使用
华为·harmonyos
轻口味4 小时前
【每日学点HarmonyOS Next知识】图片拖动、动画放大、列表高度、返回键监听、分割线颜色
华为·harmonyos·harmonyosnext