单例模式 矫情吗;* 2021-11-22 10:52 317阅读 0赞 <table> <tbody> <tr> <td style="vertical-align:top;width:.6868in;"> <p style="margin-left:0in;"><span style="color:#2b91af;"> 1 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 2 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 3 </span></p> <p><span style="color:#2b91af;"> 4 </span></p> <p><span style="color:#2b91af;"> 5 </span></p> <p><span style="color:#2b91af;"> 6 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 7 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 8 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 9 </span></p> <p><span style="color:#2b91af;"> 10 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 11 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 12 </span></p> <p><span style="color:#2b91af;"> 13 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 14 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 15 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 16 </span></p> <p><span style="color:#2b91af;"> 17 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 18 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 19 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 20 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 21 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 22 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 23 </span></p> </td> <td style="vertical-align:top;width:6.9812in;"> <p style="margin-left:0in;"><span style="color:#008000;">/**</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * @author BubbleTg</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * <p></span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * 单例模式模板01</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * 饿汉式单例模式</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * 一开始就线程安全。</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> */</span></p> <p style="margin-left:0in;"><span style="color:#0000FF;">public class</span><span style="color:#000000;"> SingletonPatternDemo01 { </span></p> <p style="margin-left:0in;"> </p> <p style="margin-left:0in;"> <span style="color:#008000;">//</span><span style="color:#008000;">初始化时立即加载</span></p> <p style="margin-left:0in;"> <span style="color:#0000FF;">private static</span><span style="color:#000000;"> SingletonPatternDemo01 instance = </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> SingletonPatternDemo01();</span></p> <p style="margin-left:0in;"> </p> <p style="margin-left:0in;"> <span style="color:#008000;">//</span><span style="color:#008000;">构造器私有化</span></p> <p style="margin-left:0in;"> <span style="color:#0000FF;">private</span><span style="color:#000000;"> SingletonPatternDemo01() { </span></p> <p style="margin-left:0in;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0in;"> </p> <p style="margin-left:0in;"> <span style="color:#008000;">//</span><span style="color:#008000;">返回</span><span style="color:#008000;">,</span><span style="color:#008000;">不需要同步,调用效率高</span></p> <p style="margin-left:0in;"> <span style="color:#0000FF;">public static</span><span style="color:#000000;"> SingletonPatternDemo01 getInstance() { </span></p> <p style="margin-left:0in;"> <span style="color:#0000FF;">return</span><span style="color:#000000;"> instance;</span></p> <p style="margin-left:0in;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0in;"> </p> <p style="margin-left:0in;"> </p> <p style="margin-left:0in;"><span style="color:#000000;">}</span></p> </td> </tr> </tbody> </table> <table> <tbody> <tr> <td style="vertical-align:top;width:.6868in;"> <p style="margin-left:0in;"><span style="color:#2b91af;"> 1 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 2 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 3 </span></p> <p><span style="color:#2b91af;"> 4 </span></p> <p><span style="color:#2b91af;"> 5 </span></p> <p><span style="color:#2b91af;"> 6 </span></p> <p><span style="color:#2b91af;"> 7 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 8 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 9 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 10 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 11 </span></p> <p><span style="color:#2b91af;"> 12 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 13 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 14 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 15 </span></p> <p><span style="color:#2b91af;"> 16 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 17 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 18 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 19 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 20 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 21 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 22 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 23 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 24 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 25 </span></p> </td> <td style="vertical-align:top;width:5.8972in;"> <p style="margin-left:0in;"><span style="color:#008000;">/**</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * @author BubbleTg</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * <p></span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * 单例模式模板01</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * 懒汉式单例模式</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * 延迟加载,真正用到的时候才加载。</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * 资源利用率高,但是由于需要同步,并发效率较低。</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> */</span></p> <p style="margin-left:0in;"><span style="color:#0000FF;">public class</span><span style="color:#000000;"> SingletonPatternDemo02 { </span></p> <p style="margin-left:0in;"> <span style="color:#0000FF;">private static</span><span style="color:#000000;"> SingletonPatternDemo02 instance;</span></p> <p style="margin-left:0in;"> </p> <p style="margin-left:0in;"> <span style="color:#008000;">//</span><span style="color:#008000;">构造器私有化</span></p> <p style="margin-left:0in;"> <span style="color:#0000FF;">private</span><span style="color:#000000;"> SingletonPatternDemo02() { </span></p> <p style="margin-left:0in;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0in;"> </p> <p style="margin-left:0in;"> <span style="color:#008000;">//</span><span style="color:#008000;">同步,调用效率低</span></p> <p style="margin-left:0in;"> <span style="color:#0000FF;">public static synchronized</span><span style="color:#000000;"> SingletonPatternDemo02 getInstance() { </span></p> <p style="margin-left:0in;"> <span style="color:#0000FF;">if</span><span style="color:#000000;"> (instance == </span><span style="color:#0000FF;">null</span><span style="color:#000000;">) { </span></p> <p style="margin-left:0in;"><span style="color:#000000;"> instance = </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> SingletonPatternDemo02();</span></p> <p style="margin-left:0in;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0in;"> <span style="color:#0000FF;">return</span><span style="color:#000000;"> instance;</span></p> <p style="margin-left:0in;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0in;"> </p> <p style="margin-left:0in;"> </p> <p style="margin-left:0in;"><span style="color:#000000;">}</span></p> <p style="margin-left:0in;"><span style="color:#000000;"> </span></p> <p style="margin-left:0in;"><span style="color:#000000;"> </span></p> <p style="margin-left:0in;"><span style="color:#000000;"> </span></p> </td> </tr> </tbody> </table> 必须知道线程同步问题!!! <table> <tbody> <tr> <td style="vertical-align:top;width:.6868in;"> <p style="margin-left:0in;"><span style="color:#2b91af;"> 1 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 2 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 3 </span></p> <p><span style="color:#2b91af;"> 4 </span></p> <p><span style="color:#2b91af;"> 5 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 6 </span></p> <p><span style="color:#2b91af;"> 7 </span></p> <p><span style="color:#2b91af;"> 8 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 9 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 10 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 11 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 12 </span></p> <p><span style="color:#2b91af;"> 13 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 14 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 15 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 16 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 17 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 18 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 19 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 20 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 21 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 22 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 23 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 24 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 25 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 26 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 27 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 28 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 29 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 30 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 31 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 32 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 33 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 34 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 35 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 36 </span></p> </td> <td style="vertical-align:top;width:5.5638in;"> <p style="margin-left:0in;"><span style="color:#008000;">/**</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * @author BubbleTg</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * <p></span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * 单例模式模板03</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * 双重实现锁实现单例模式</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * <p></span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * 此种会出现的问题</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * 因为编译器的优化原因和JVM底层内部模型原因,偶尔会出问题,不建议使用。</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> */</span></p> <p style="margin-left:0in;"><span style="color:#0000FF;">public class</span><span style="color:#000000;"> SingletonPatternDemo03 { </span></p> <p style="margin-left:0in;"> <span style="color:#0000FF;">private static</span><span style="color:#000000;"> SingletonPatternDemo03 instance = </span><span style="color:#0000FF;">null</span><span style="color:#000000;">;</span></p> <p style="margin-left:0in;"> </p> <p style="margin-left:0in;"> <span style="color:#008000;">//</span><span style="color:#008000;">构造器私有化</span></p> <p style="margin-left:0in;"> <span style="color:#0000FF;">private</span><span style="color:#000000;"> SingletonPatternDemo03() { </span></p> <p style="margin-left:0in;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0in;"> </p> <p style="margin-left:0in;"> </p> <p style="margin-left:0in;"> <span style="color:#0000FF;">public static</span><span style="color:#000000;"> SingletonPatternDemo03 getInstance() { </span></p> <p style="margin-left:0in;"> <span style="color:#0000FF;">if</span><span style="color:#000000;"> (instance == </span><span style="color:#0000FF;">null</span><span style="color:#000000;">) { </span></p> <p style="margin-left:0in;"><span style="color:#000000;"> SingletonPatternDemo03 sp;</span></p> <p style="margin-left:0in;"> <span style="color:#0000FF;">synchronized</span><span style="color:#000000;"> (SingletonPatternDemo03.</span><span style="color:#0000FF;">class</span><span style="color:#000000;">) { </span></p> <p style="margin-left:0in;"><span style="color:#000000;"> sp = instance;</span></p> <p style="margin-left:0in;"> <span style="color:#0000FF;">if</span><span style="color:#000000;"> (sp == </span><span style="color:#0000FF;">null</span><span style="color:#000000;">) { </span></p> <p style="margin-left:0in;"> <span style="color:#0000FF;">synchronized</span><span style="color:#000000;"> (SingletonPatternDemo03.</span><span style="color:#0000FF;">class</span><span style="color:#000000;">) { </span></p> <p style="margin-left:0in;"> <span style="color:#0000FF;">if</span><span style="color:#000000;"> (sp == </span><span style="color:#0000FF;">null</span><span style="color:#000000;">) { </span></p> <p style="margin-left:0in;"><span style="color:#000000;"> sp = </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> SingletonPatternDemo03();</span></p> <p style="margin-left:0in;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0in;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0in;"><span style="color:#000000;"> instance = sp;</span></p> <p style="margin-left:0in;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0in;"> </p> <p style="margin-left:0in;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0in;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0in;"> <span style="color:#0000FF;">return</span><span style="color:#000000;"> instance;</span></p> <p style="margin-left:0in;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0in;"><span style="color:#000000;">}</span></p> </td> </tr> </tbody> </table> <table> <tbody> <tr> <td style="vertical-align:top;width:.6868in;"> <p style="margin-left:0in;"><span style="color:#2b91af;"> 1 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 2 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 3 </span></p> <p><span style="color:#2b91af;"> 4 </span></p> <p><span style="color:#2b91af;"> 5 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 6 </span></p> <p><span style="color:#2b91af;"> 7 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 8 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 9 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 10 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 11 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 12 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 13 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 14 </span></p> <p><span style="color:#2b91af;"> 15 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 16 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 17 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 18 </span></p> <p><span style="color:#2b91af;"> 19 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 20 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 21 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 22 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 23 </span></p> </td> <td style="vertical-align:top;"> <p style="margin-left:0in;"><span style="color:#008000;">/**</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * @author BubbleTg</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * <p></span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * 单例模式模板04</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * 静态内部类实现单例模式</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * <p></span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * 也是一种懒加载,兼备了并发高效和延迟加载的优势!</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> */</span></p> <p style="margin-left:0in;"><span style="color:#0000FF;">public class</span><span style="color:#000000;"> SingletonPatternDemo04 { </span></p> <p style="margin-left:0in;"> </p> <p style="margin-left:0in;"> <span style="color:#0000FF;">private static class</span><span style="color:#000000;"> SingletonPatternInstance { </span></p> <p style="margin-left:0in;"> <span style="color:#0000FF;">private static final</span><span style="color:#000000;"> SingletonPatternDemo04 instance = </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> SingletonPatternDemo04();</span></p> <p style="margin-left:0in;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0in;"> </p> <p style="margin-left:0in;"> <span style="color:#008000;">//</span><span style="color:#008000;">构造器私有化</span></p> <p style="margin-left:0in;"> <span style="color:#0000FF;">private</span><span style="color:#000000;"> SingletonPatternDemo04() { </span></p> <p style="margin-left:0in;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0in;"> </p> <p style="margin-left:0in;"> <span style="color:#008000;">//</span><span style="color:#008000;">方法没有同步,调用效率高!</span></p> <p style="margin-left:0in;"> <span style="color:#0000FF;">public static</span><span style="color:#000000;"> SingletonPatternDemo04 getInstance() { </span></p> <p style="margin-left:0in;"> <span style="color:#0000FF;">return</span><span style="color:#000000;"> SingletonPatternInstance.instance;</span></p> <p style="margin-left:0in;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0in;"><span style="color:#000000;">}</span></p> </td> </tr> </tbody> </table> <table> <tbody> <tr> <td style="vertical-align:top;width:.6868in;"> <p style="margin-left:0in;"><span style="color:#2b91af;"> 1 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 2 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 3 </span></p> <p><span style="color:#2b91af;"> 4 </span></p> <p><span style="color:#2b91af;"> 5 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 6 </span></p> <p><span style="color:#2b91af;"> 7 </span></p> <p><span style="color:#2b91af;"> 8 </span></p> <p><span style="color:#2b91af;"> 9 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 10 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 11 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 12 </span></p> <p><span style="color:#2b91af;"> 13 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 14 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 15 </span></p> <p><span style="color:#2b91af;"> 16 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 17 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 18 </span></p> <p style="margin-left:0in;"><span style="color:#2b91af;"> 19 </span></p> </td> <td style="vertical-align:top;width:3.8687in;"> <p style="margin-left:0in;"><span style="color:#008000;">/**</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * @author BubbleTg</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * <p></span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * 单例模式模板05</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * 枚举实现单例模式</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * <p></span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * 缺点:没有延迟加载</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * 优点:</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> * 线程安全,调用效率高,天然防止反射和反序列化漏洞!</span></p> <p style="margin-left:0in;"><span style="color:#008000;"> */</span></p> <p style="margin-left:0in;"><span style="color:#0000FF;">public enum</span><span style="color:#000000;"> SingletonPatternDemo05 { </span></p> <p style="margin-left:0in;"> </p> <p style="margin-left:0in;"> <span style="color:#008000;">//</span><span style="color:#008000;">这个枚举元素,本身就是单例</span></p> <p style="margin-left:0in;"><span style="color:#000000;"> INSTANCE;</span></p> <p style="margin-left:0in;"> </p> <p style="margin-left:0in;"> <span style="color:#008000;">//</span><span style="color:#008000;">添加自己需要的操作</span></p> <p style="margin-left:0in;"> <span style="color:#0000FF;">public void</span><span style="color:#000000;"> singletonOperation(){ </span></p> <p style="margin-left:0in;"><span style="color:#000000;"> }</span></p> <p style="margin-left:0in;"><span style="color:#000000;">}</span></p> </td> </tr> </tbody> </table> \-常见的五种单例模式实现方式 \-主要: \-饿汉式(线程安全,调用效率高。但是,不能延时加载。) •懒汉式(线程安全,调用效率不高。但是,可以延时加载。) \-其他: •双重检测锁式(由于JVM底层内部模型原因,偶尔会出问题。不建议使用) •静态内部类式(线程安全,调用效率高。但是,可以延时加载) •枚举式(线程安全,调用效率高,不能延时加载。并且可以天然的防止反射和反序列 化漏洞!) •如何选用? \-单例对象占用资源少,不需要延时加载: •枚举式好于饿汉式 \-单例对象占用资源大,需要延时加载: •静态内部类式 好于 懒汉式
相关 单例模式 http://blog.csdn.net/zhengzhb/article/details/7331369 定义:确保一个类只有一个实例,而且自行实例化并向整个系统提供这个实 曾经终败给现在/ 2022年09月25日 15:30/ 0 赞/ 264 阅读
相关 单例模式 class sigle{ protected static $ins = null; public function getIns(){ 深藏阁楼爱情的钟/ 2022年07月20日 20:27/ 0 赞/ 285 阅读
相关 单例模式 简介: 单例模式是一种常用的软件设计模式,其定义是单例对象的类只能允许一个实例存在。 许多时候整个系统只需要拥有一个的全局对象,这样有利于我们协调系统整体的行为。比如在 ﹏ヽ暗。殇╰゛Y/ 2022年05月09日 15:46/ 0 赞/ 178 阅读
相关 单例模式 <table> <tbody> <tr> <td style="vertical-align:top;width:.6868in;"> <p style 矫情吗;*/ 2021年11月22日 10:52/ 0 赞/ 318 阅读
相关 单例模式 单例模式 单例模式(SingletonPattern)是java中最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。 这种模式涉及 红太狼/ 2021年11月16日 05:30/ 0 赞/ 338 阅读
相关 单例模式 1、懒汉式,线程不安全 是否 Lazy 初始化:是 是否多线程安全:否 实现难度:易 描述:这种方式是最基本的实现方式,这种实现最大的问题就是不支持多线程。因为没有 以你之姓@/ 2021年11月11日 17:44/ 0 赞/ 301 阅读
相关 单例模式 应用场景 1. windows的任务管理器 2. 网站的计数器 3. 应用程序的日志 4. 数据库连接池,因为数据库连接是一种数据库资源。数据库软件系统中使用数据 r囧r小猫/ 2021年11月11日 15:08/ 0 赞/ 376 阅读
相关 单例模式 单例模式有以下特征: 1. 只有一个对象存在 2. 对象的实例化必须在类中实现 一、懒汉模式(线程不安全) package com.kevin; 谁借莪1个温暖的怀抱¢/ 2021年10月01日 07:48/ 0 赞/ 344 阅读
相关 单例模式 1.定义 单例模式是一种常用的软件设计模式,其定义是单例对象的类只能允许一个实例存在。 2.实现步骤 1. 将该类的构造方法定义为私有方法,这样其他处 小咪咪/ 2021年09月27日 13:56/ 0 赞/ 426 阅读
相关 单例模式 单例模式:保证一个类仅有一个实例,并提供一个访问它的全局访问点。 通常我们可以让一个全局变量使得一个对象被访问,但它不能防止你实例化多个对象。一个最好的办法就是,让类自身负责 Dear 丶/ 2021年09月17日 02:10/ 0 赞/ 360 阅读
还没有评论,来说两句吧...