zoukankan      html  css  js  c++  java
  • java单列设计模式

    java] view plain copy
     
    1. <h6><span style="font-size:18px; background-color:rgb(255,255,255); color:rgb(51,51,51); line-height:26px; white-space:pre-wrap">当一个类只能有一个对象时,往往会用到单例模式,例如,现实生活中有很多临界资源,像 打印机、处理器(单核)、皇帝、太子等等,它们都是稀有资源,只能有一个实例对象。下面用java将单例模式实现:</span></h6>  
        1.利用枚举类型实现(单多线程均可)
    [java] view plain copy
     
    1. public enum SingletonWithEnum {  
    2.     /** 
    3.      * @author boker 
    4.      */  
    5.     instance;  
    6.     public static SingletonWithEnum getInstance() {  
    7.           
    8.         return instance;  
    9.     }  
    10. }  
    原理是:枚举类型当只有一个成员时,就是一个最简单的单例模式实现方式。
    Effective Java作者Josh Bloch 提倡这种方式,它不仅能避免多线程同步问题,而且还能防止反序列化重新创建新的对象,但很少有人这么用,或许是应为enum在jdk1.5之后才出来,大家都已经用习惯用常规的设计模式实现了。
    
        2.单线程实现(非线程安全,不适用与多线程)
    	   1> 定义一个类(该类最好定义成final类型的可以防止被继承)
    	2>声明一个private的该类 类型的静态实例成员instance
    	3>实现一个private的构造函数
    	4>实现一个public静态方法getInstance(),在其中调用私有构造函数创建该类的一个实例,返回给调用者
    代码实现如下:
    [java] view plain copy
     
    1. public class Singleton {  
    2.   
    3.     /** 
    4.      * @author boker 
    5.      */  
    6.     private static Singleton instance;  
    7.   
    8.     private Singleton() {  
    9.   
    10.     }  
    11.   
    12.     public static Singleton getInstance() {  
    13.         if (instance == null) {  
    14.             instance = new Singleton();  
    15.         } else {  
    16.             return instance;  
    17.         }  
    18.         return instance;  
    19.     }  
    20. }  
        3.线程安全的实现方式(适用于多线程,但效率低)
    [java] view plain copy
     
    1. public class Singleton3 {  
    2.   
    3.     /** 
    4.      * @author boker 
    5.      */  
    6.     private static Singleton3 instance = null;  
    7.   
    8.     private Singleton3() {}  
    9.   
    10.     public static synchronized Singleton3 getInstance() {  
    11.           
    12.         return instance==null?instance=new Singleton3():instance;  
    13.     }  
    14. }  
        4.双重校验锁( 适用于jdk1.5之后)
    [java] view plain copy
     
    1. public class Singleton4 {  
    2.   
    3.     /** 
    4.      * @author boker 
    5.      */  
    6.     private volatile static Singleton4 instance;  
    7.   
    8.     private Singleton4() {}  
    9.   
    10.     public static Singleton4 getInstance() {  
    11.         if (instance==null) {  
    12.             synchronized (Singleton4.class) {  
    13.                 if (instance==null) {  
    14.                       
    15.                     instance = new Singleton4();  
    16.                 }  
    17.             }  
    18.         }   
    19.         return instance;  
    20.     }  
    21. }  
  • 相关阅读:
    CSS的扩展less和sass
    html5小游戏基础知识
    htm5拖放和画布
    htm5
    并查集模板
    二叉树的建树
    kmp的书写
    贪心算法
    容器
    POJ2442 优先队列
  • 原文地址:https://www.cnblogs.com/liuhai35/p/6000450.html
Copyright © 2011-2022 走看看