单例模式在Android非常常见,例如user、networkManager、数据库操作等等。一般使用dcl或者静态内部类单例,带参数选择dcl,不带参数静态内部类。
1、java单例5种写法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| public class SingleInstance {
static class SingleInstanceAdd { private SingleInstanceAdd() { }
private static SingleInstanceAdd instanceAdd = new SingleInstanceAdd();
public static SingleInstanceAdd getInstance() { return instanceAdd; }
@Override public String toString() { return super.toString() + " add completed"; } }
static class SingleInstanceLazy { private SingleInstanceLazy() { }
private static SingleInstanceLazy singleInstanceLazy = null;
public static SingleInstanceLazy getInstance() { if (singleInstanceLazy == null) { singleInstanceLazy = new SingleInstanceLazy(); } return singleInstanceLazy; }
@Override public String toString() { return super.toString() + " lazy complete"; } }
static class SingleInstanceSynchronized { private SingleInstanceSynchronized() { }
private static SingleInstanceSynchronized singleInstanceSynchronized = null;
public static synchronized SingleInstanceSynchronized getInstance() { if (singleInstanceSynchronized == null) { singleInstanceSynchronized = new SingleInstanceSynchronized(); } return singleInstanceSynchronized; } }
static class SingleInstanceDCL { private SingleInstanceDCL() { }
private static SingleInstanceDCL singleInstanceDCL = null;
public static SingleInstanceDCL getInstance() { if (singleInstanceDCL == null) { synchronized (SingleInstanceDCL.class) { if (singleInstanceDCL == null) singleInstanceDCL = new SingleInstanceDCL(); } } return singleInstanceDCL; } }
static class SingleInstanceStatic{ private SingleInstanceStatic(){} private static class SingleStatic{ private static SingleInstanceStatic singleInstanceStatic = new SingleInstanceStatic(); } public SingleInstanceStatic getInstance(){ return SingleStatic.singleInstanceStatic; } }
public static void main(String[] args) { print(SingleInstanceAdd.getInstance().toString()); print(SingleInstanceLazy.getInstance().toString()); }
static void print(String message) { System.out.println(message); } }
|
2、kotlin单例5种写法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| class SingleInstanceDCL { }
private object SingleInstance { override fun toString(): String { println(super.toString() + " " + javaClass.name + " complete") return super.toString() } }
private class SingleInstanceLazy { companion object { val instanceLazy by lazy(LazyThreadSafetyMode.NONE) { SingleInstanceLazy() } }
override fun toString(): String { println(super.toString() + " " + javaClass.name + " complete") return super.toString() } }
private class SingleInstanceSynchronized{ companion object { private var singleInstanceSynchronized:SingleInstanceSynchronized?=null @Synchronized fun get():SingleInstanceSynchronized{ if (null== singleInstanceSynchronized) singleInstanceSynchronized = SingleInstanceSynchronized() return singleInstanceSynchronized as SingleInstanceSynchronized } } }
private class SingleInstanceDCLkt{ companion object{ val instanceDCL by lazy(LazyThreadSafetyMode.SYNCHRONIZED){ SingleInstanceDCL() } } }
private class SingleInstanceStatic{ private object SingleStatic{ val instanceStatic = SingleInstanceStatic() } companion object { fun getInstance() = SingleStatic.instanceStatic } }
private fun main() { SingleInstance.toString() SingleInstanceLazy.instanceLazy.toString() SingleInstanceSynchronized.get().toString() }
|