Android--单例模式
发布日期:2021-05-10 12:49:13 浏览次数:26 分类:精选文章

本文共 2752 字,大约阅读时间需要 9 分钟。

������������������������������������������������������

���������������Singleton Pattern���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������������

���������������������������������������������������������greedy singleton������������������lazy singleton������

���������

������������Greedy Singleton������������������������������������������������������������������������������������������������������������������������������������������������������������������������

private static Singleton uniqueInstance = new Singleton();

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������

������������Lazy Singleton���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

������������������������������������������������������������������������Double Checked Locking���������������

public static synchronized Singleton getInstance() {    if (instance == null) {        synchronized (Singleton.class) {            if (instance == null) {                instance = new Singleton();            }        }    }    return instance;}

���������������������������������������������������������������volatile���

private volatile static Singleton instance = null;

������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

package example6;public enum Singleton {    UNIQUE_INSTANCE;}

���������������������������������������������������������������������������������������������������������������������������������������

������������������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

上一篇:Android Binder机制原理(史上最强理解,没有之一)
下一篇:Android--进程间通信(Binder)

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2025年04月28日 09时51分21秒