HarmonyOS鸿蒙Next中单例模式
HarmonyOS鸿蒙Next中单例模式
单例模式
创建者模式:主要关注点是“怎样创建对象”,特点是“将对象的创建和使用分离”,可以降低系统的耦合度
单例类:只能创建一个实例的类 1.私有化构造方法 2.声明变量 3.对外提供访问方式
单例模式可以分为饿汉式和懒汉式:
饿汉式:类在加载时,该单实例对象就已经被创建,但是可能存在内容浪费的问题
静态变量方式 静态代码块方式 枚举方式(在不考虑内存浪费的情况下首选枚举方式,是唯一不会被破坏的实现方式)
//静态变量方式
public class Singleton {
private Singleton() {
}
private static Singleton instance=new Singleton();//初始化
//对外提供了访问对象
public static Singleton getInstance(){
return instance;
}
}
//静态代码块方式
public class Singleton {
private Singleton() {
}
private static Singleton instance
static{
instance=new Singleton
}
//对外提供了访问对象
public static Singleton getInstance(){
return instance;
}
}
//测试
public static void main(String[] args) {
Singleton singleton=Singleton.getInstance();
Singleton singleton1=Singleton.getInstance();
System.out.println(singleton1==singleton);
}
//枚举方式
public enum Singleton{
INSTANCE;
}
//测试
懒汉式:只有类在调用时,才会创建实例
方式1:线程不安全 方式2:线程安全 方式3:双重检查锁(后进的线程就可以不用加锁了,判断instance不为null就直接return) 方式4:静态内部类
public class Singleton {
private Singleton() {
}
private static Singleton instance;
//加同步锁保证线程安全
public static synchronized Singleton getInstance(){
//非空判断
if(instance==null){
instance=new Singleton();
}
return instance;
}
}
//双重检查锁
//由于多线程每次都加锁影响性能,可以先对instance进行判断再决定要不要加锁
public class Singleton {
private Singleton() {
}
private static volatile Singleton instance;//多线程情况下,可能会出现空指针问题,所以要加volatile保证可见性和有序性
public static Singleton getInstance(){
//非空判断
if(instance==null){
synchronized(Singleton.class){
if(instance==null){
instance=new Singleton();
}
}
}
return instance;
}
}
//静态内部类
public class Singleton {
private Singleton() {
}
private static class singletonHolder {
private static final Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance() {
return singletonHolder.INSTANCE;
}
}
破坏单例模式:序列化和反射
序列化:就是把“内存里的对象”变成“可以保存或传输的字节”,反序列化就是再把它还原回对象
public class Singleton implements Serializable {//实现序列化的接口才能进行序列化
private Singleton(){}
private static class SingletonHolder{
private static final Singleton INSTANCE=new Singleton();
}
public static Singleton getInstance(){
return SingletonHolder.INSTANCE;
}
}
//
public class Client {
public static void main(String[] args) throws Exception {
//writeObjectToFile();
readObjectFromFile();
readObjectFromFile();
}
public static void writeObjectToFile() throws Exception {
Singleton instance = Singleton.getInstance();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("C:\\Users\\yyz20\\Desktop\\a.txt"));
oos.writeObject(instance);
oos.close();
}
public static void readObjectFromFile() throws Exception {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("C:\\Users\\yyz20\\Desktop\\a.txt"));
Singleton instance = (Singleton) ois.readObject();
ois.close();
System.out.println(instance);
}
}
解决方法:在Singleton中添加一个方法:
//当进行反序列化时,会自动调用该方法,将该方法的返回值直接返回
public Object readResolve(){
return SingletonHolder.INSTANCE;
}
反射:
public static void main(String[] args) throws Exception {
Class clazz=Singleton.class;//获取字节码文件
Constructor cons=clazz.getDeclaredConstructor();//获取无参构造方法对象
cons.setAccessible(true);//取消访问检查
Singleton s1=(Singleton) cons.newInstance();//创建新的实例
Singleton s2=(Singleton) cons.newInstance();
System.out.println(s1==s2);
}
解决方法:在无参构造中加判断,如果多次创建就抛出异常,并且为了避免出现线程安全还要加锁
private static boolean flag=false;
private Singleton(){
synchronized(Singleton.class) {
if (flag) {
throw new RuntimeException("不能创建多个对象");
}
flag = true;
}
}
更多关于HarmonyOS鸿蒙Next中单例模式的实战教程也可以访问 https://www.itying.com/category-93-b0.html
HarmonyOS Next的单例模式通过ArkTS实现,使用static
实例和私有构造器确保全局唯一实例。示例代码:
class Singleton {
private static instance: Singleton = new Singleton();
private constructor() {}
static getInstance(): Singleton {
return this.instance;
}
}
通过getInstance()
方法获取实例,构造函数私有化防止外部实例化。这种方式在应用生命周期内保持单例状态,适用于状态管理或全局服务场景。
更多关于HarmonyOS鸿蒙Next中单例模式的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS Next中,单例模式的设计和实现与标准Java方式基本一致,但需结合ArkTS语言特性和系统架构进行适配。
饿汉式实现(静态变量方式):
class Singleton {
private static instance: Singleton = new Singleton();
private constructor() {}
public static getInstance(): Singleton {
return Singleton.instance;
}
}
懒汉式实现(双重检查锁):
class Singleton {
private static instance: Singleton | null = null;
private constructor() {}
public static getInstance(): Singleton {
if (!Singleton.instance) {
synchronized(Singleton) {
if (!Singleton.instance) {
Singleton.instance = new Singleton();
}
}
}
return Singleton.instance!;
}
}
枚举方式(推荐):
enum Singleton {
INSTANCE;
// 可添加实例方法
public doSomething(): void {
// 业务逻辑
}
}
在HarmonyOS Next中需注意:
- 使用ArkTS的类语法而非Java
- 类型注解需明确(如
Singleton | null
) - 同步机制需使用HarmonyOS提供的并发原语
- 枚举实现最为简洁且线程安全
序列化和反射问题在HarmonyOS生态中较少遇到,但设计时仍需考虑单例的完整性保护。