美文网首页
【最佳实践·Java Practice】Defensive Co

【最佳实践·Java Practice】Defensive Co

作者: Deeglose | 来源:发表于2017-10-01 15:42 被阅读32次

参考

http://www.javapractices.com

Defensive Copy

当传出一个内部对象的引用时,如果传出的对象会被修改从而导致内部状态修改,而这并不是所期望的行为,则应当传出一个内部对象的复制,保证其所有修改都不会影响到内部对象。这就是“防卫性复制”。
注意:必须满足 1.类本身要求为Immutable的 2.get方法传出的对象会被修改

例子:一个Planet类,它是不可变的。它有一个不可变域对象:fDateOfDiscovery。下面的代码展示了在构造函数中如何初始化该域,以及在get方法中如何返回该域。

import java.util.Date;

/**
* Planet is an immutable class, since there is no way to change
* its state after construction.
*/
public final class Planet {

  public Planet (double aMass, String aName, Date aDateOfDiscovery) {
     fMass = aMass;
     fName = aName;
     //make a private copy of aDateOfDiscovery
     //this is the only way to keep the fDateOfDiscovery
     //field private, and shields this class from any changes that 
     //the caller may make to the original aDateOfDiscovery object
     fDateOfDiscovery = new Date(aDateOfDiscovery.getTime());
  }

  /**
  * Returns a primitive value.
  *
  * The caller can do whatever they want with the return value, without 
  * affecting the internals of this class. Why? Because this is a primitive 
  * value. The caller sees its "own" double that simply has the
  * same value as fMass.
  */
  public double getMass() {
    return fMass;
  }

  /**
  * Returns an immutable object.
  *
  * The caller gets a direct reference to the internal field. But this is not 
  * dangerous, since String is immutable and cannot be changed.
  */
  public String getName() {
    return fName;
  }

//  /**
//  * Returns a mutable object - likely bad style.
//  *
//  * The caller gets a direct reference to the internal field. This is usually dangerous, 
//  * since the Date object state can be changed both by this class and its caller.
//  * That is, this class is no longer in complete control of fDate.
//  */
//  public Date getDateOfDiscovery() {
//    return fDateOfDiscovery;
//  }

  /**
  * Returns a mutable object - good style.
  * 
  * Returns a defensive copy of the field.
  * The caller of this method can do anything they want with the
  * returned Date object, without affecting the internals of this
  * class in any way. Why? Because they do not have a reference to 
  * fDate. Rather, they are playing with a second Date that initially has the 
  * same data as fDate.
  */
  public Date getDateOfDiscovery() {
    return new Date(fDateOfDiscovery.getTime());
  }

  // PRIVATE

  /**
  * Final primitive data is always immutable.
  */
  private final double fMass;

  /**
  * An immutable object field. (String objects never change state.)
  */
  private final String fName;

  /**
  * A mutable object field. In this case, the state of this mutable field
  * is to be changed only by this class. (In other cases, it makes perfect
  * sense to allow the state of a field to be changed outside the native
  * class; this is the case when a field acts as a "pointer" to an object
  * created elsewhere.)
  */
  private final Date fDateOfDiscovery;
}
 

参考:http://www.javapractices.com/topic/TopicAction.do?Id=15

相关文章

网友评论

      本文标题:【最佳实践·Java Practice】Defensive Co

      本文链接:https://www.haomeiwen.com/subject/eghaextx.html