CodeWorldkotlin带参数的单例模式封装实现

Posted guangdeshishe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeWorldkotlin带参数的单例模式封装实现相关的知识,希望对你有一定的参考价值。

带参数单例模式双重检验实现:

//带参数单例模式:双重检验
class Singleton5 private constructor() 
    private lateinit var mContext: Context

    private constructor(context: Context) : this() 
        mContext = context.applicationContext
    

    companion object 
        @Volatile
        private var instance: Singleton5? = null
        fun getInstance(context: Context): Singleton5 
            return instance ?: synchronized(this) 
                instance ?: Singleton5(context)
            
        
    
    fun test() 

// 使用
Singleton5.getInstance(context).test()

带参数单例模式封装实现

//基类
open class BaseSingletonHolder<out instanceClass, in paramClass> constructor(val constructor: (paramClass) -> instanceClass) 
    @Volatile
    private var mInstance: instanceClass? = null
    fun getInstance(param: paramClass): instanceClass 
        return mInstance ?: synchronized(this) 
            mInstance ?: constructor(param).apply 
                mInstance = this
            
        
    

//实现类1
class Singleton6 private constructor(context: Context) 
    companion object : BaseSingletonHolder<Singleton6, Context>(::Singleton6) 

    fun test() 

//实现类2
class Singleton7 private constructor(param: String) 
    companion object : BaseSingletonHolder<Singleton7 , String>(::Singleton7 ) 

    fun test() 

//使用
Singleton6.getInstance(context).test()
Singleton7.getInstance(context).test()

不带参数单例模式不同实现方式小结

饿汉式

//饿汉式1
class Singleton private constructor() 
    companion object 
        var instance: Singleton = Singleton()
    


//饿汉式2
object SingleTon0 


懒汉式

class Singleton1 private constructor() 
    companion object 
        var instance: Singleton1? = null
            get() 
                if (field == null) 
                    instance = Singleton1()
                
                return field
            
    

线程安全:双重检验

//线程安全:双重检验1
class Singleton2 private constructor() 
    companion object 
        @Volatile
        var instance: Singleton2? = null
            get() 
                if (field == null) 
                    synchronized(this) 
                        if (field == null) 
                            field = Singleton2()
                        
                    
                
                return field
            
    


//线程安全:双重检验2
class Singleton3 private constructor() 
    companion object 
        val instance: Singleton3 by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) 
            Singleton3()
        
    

线程安全:静态内部类

//线程安全:静态内部类
class Singleton4 private constructor() 
    companion object 
        val instance: Singleton4
            get() 
                return InstanceHolder.instance
            

        private class InstanceHolder 
            companion object 
                val instance = Singleton4()
            
        
    

以上是关于CodeWorldkotlin带参数的单例模式封装实现的主要内容,如果未能解决你的问题,请参考以下文章

CodeWorldkotlin带参数的单例模式封装实现

带参数的单例

Kotlin 中带参数的单例

OKHTTP的单例和再封装

OKHTTP的单例和再封装

spring 的单例模式