package com.dahe.gldriver.utils

/**
 * @ClassName BaseSingleton
 * @Author 用户
 * @Date 2023/12/29 09:49
 * @Description //要实现单例类，就只需要继承这个 BaseSingleton 即可
                //P为参数,T为返回值
 */
abstract class BaseSingleton<in P, out T> {

    @Volatile
    private var instance: T? = null

    //抽象方法，需要我们在具体的单例子类当中实现此方法
    protected abstract fun creator(param: P?): T

    fun getInstance(param: P?): T =
        instance ?: synchronized(this) {
            instance ?: creator(param).also { instance = it }
        }

}

abstract class BaseSingNoParam<out T> {

    @Volatile
    private var instance: T? = null

    //抽象方法，需要我们在具体的单例子类当中实现此方法
    protected abstract fun creator(): T

    fun getInstance(): T =
        instance ?: synchronized(this) {
            instance ?: creator().also { instance = it }
        }

}