EI_EXPOSE_REP2

EI2: Expose internal representation by storing an externally mutable object

This code stores a reference to an externally mutable object into the internal representation of the object. If instances are accessed by untrusted code, and unchecked changes to the mutable object would compromise security or other important properties, you will need to do something different. Storing a copy of the object is better approach in many situations.

將mutable object直接assign,會被怎樣存取無法得知,可能會造成來源一起被修改。

在assign時,使用copy constructor去建立一份新的拷貝。

Before:

   public void setLeftDate(Date aDate){
       mLeftDate = aDate;
   }

After:

   public void setLeftDate(Date aDate){
       mLeftDate = aDate != null ? new Date(aDate.getTime()) : aDate;
   }

因為直接使用getTime,必須小心傳入值為null。