单例模式及单例对象的可序列化

        为了使一个单例类变成可串行化的,仅仅在声明中添加“implements Serializable”是不够的。因为一个串行化的对象在每次返串行化的时候,都会创建一个新的对象,而不仅仅是一个对原有对象的引用。为了防止这种情况,可以在单例类中加入readResolve 方法。

        下面我们先简要地回顾下对象的序列化. 一般来说, 一个类实现了 Serializable接口, 我们就可以把它往内存地写再从内存里读出而"组装"成一个跟原来一模一样的对象. 不过当序列化遇到单例时,这里边就有了个问题: 从内存读出而组装的对象破坏了单例的规则. 单例是要求一个JVM中只有一个类对象的, 而现在通过反序列化,一个新的对象克隆了出来.

如下例所示:  

  •   Java代码     
  •   
  • public final class MySingleton implements Serializable {  
  •      private MySingleton() { }  
  •      private static final MySingleton INSTANCE = new MySingleton();  
  •      public static MySingleton getInstance() { return INSTANCE; }  
  • }  
  •  
  • 当把 MySingleton对象(通过getInstance方法获得的那个单例对象)序列化后再从内存中读出时, 就有一个全新但跟原来一样的MySingleton对象存在了. 那怎么来维护单例模式呢?这就要用到readResolve方法了.如下所示: 

    Java代码   

  •   
  • public final class MySingleton implements Serializable{  
  •     private MySingleton() { }  
  •     private static final MySingleton INSTANCE = new MySingleton();  
  •     public static MySingleton getInstance() { return INSTANCE; }  
  •     private Object readResolve() throws ObjectStreamException {  
  •        // instead of the object we're on,  
  •        // return the class variable INSTANCE  
  •       return INSTANCE;  
  •    }  

  • 这样当JVM从内存中反序列化地"组装"一个新对象时,就会自动调用这个 readResolve方法来返回我们指定好的对象了, 单例规则也就得到了保证 。


  • 下面的这个例子 更能很好的说明  
  • readResove的必要性  
  • So far so good. Things get a little complicated when dealing with more than one instance however. To explain this, I'll show this using a type-safe enumeration. Keep in mind that Java 5'enum type automatically handles this readResolve case for you. Here is a nice little enumeration:   
  •   
  •   
  • public final class Sides {  
  •  private int value;  
  •  private Sides(int newVal) { value = newVal; }  
  •  private static final int LEFT_VALUE = 1;  
  •  private static final int RIGHT_VALUE = 2;  
  •  private static final int TOP_VALUE = 3;  
  •  private static final int BOTTOM_VALUE = 4;  
  •    
  •  public static final LEFT = new Sides(LEFT_VALUE);  
  •  public static final RIGHT = new Sides(RIGHT_VALUE);  
  •  public static final TOP = new Sides(TOP_VALUE);  
  •  public static final BOTTOM = new Sides(BOTTOM_VALUE);  
  •    
  • }  
  •   
  •   
  • Now, implementing serialization, the key to determining which instance to return is in inspecting what value is set on the object itself:   
  •   
  •   
  • public final class Sides implements Serializable {  
  •  private int value;  
  •  private Sides(int newVal) { value = newVal; }  
  •  private static final int LEFT_VALUE = 1;  
  •  private static final int RIGHT_VALUE = 2;  
  •  private static final int TOP_VALUE = 3;  
  •  private static final int BOTTOM_VALUE = 4;  
  •    
  •  public static final LEFT = new Sides(LEFT_VALUE);  
  •  public static final RIGHT = new Sides(RIGHT_VALUE);  
  •  public static final TOP = new Sides(TOP_VALUE);  
  •  public static final BOTTOM = new Sides(BOTTOM_VALUE);  
  •    
  •  private Object readResolve() throws ObjectStreamException {  
  •   // Switch on this instance's value to figure out which class variable  
  •   // this is meant to match  
  •   switch(value) {  
  •    case LEFT_VALUE: return LEFT;  
  •    case RIGHT_VALUE: return RIGHT;  
  •    case TOP_VALUE: return TOP;  
  •    case BOTTOM_VALUE: return BOTTOM;    
  •   }  
  •   return null;  
  •  }  



  • 题外话,介绍一种更简单的创建单例对象的方法:

  • public static final Singleton INSTANCE = new Singleton();


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值