android 开发中应用到的单例模式

目的:

希望对象只创建一个实例,并且提供一个全局的访问点。

 

 

public class SingletonF implements Serializable {    

         private static class SingletonHolder {    

          /**  

           * 单例对象实例  

           */    

         static final SingletonF INSTANCE = new SingletonF();    

         }    

   

         public static SingletonF getInstance() {    

                return SingletonHolder.INSTANCE;    

         }    

   

         /**

          * private的构造函数用于避免外界直接使用new来实例化对象  

          */  

        private SingletonF() {  

        }    

        /**

         * readResolve方法应对单例对象被序列化时候  

         */    

        private Object readResolve() {  

             return getInstance();  

        }  

   }    

posted on 2016-03-11 16:44  yuLook  阅读(276)  评论(0编辑  收藏  举报

导航