ImmutableObject < Javapedia < TWiki
|
TWiki . Javapedia . ImmutableObject
|
Immutable Object
Objects have identity (location in memory), state (data), and behavior (methods). Once constructed, an Immutable Object cannot change state.
Immutable objects greatly simplify your program, since they
- are simple to construct, test, and use
- are automatically thread-safe and have no synchronization issues
- do not need a copy constructor
- do not need an implementation of
clone
- do not need to be copied defensively when used as a field
- make good Map keys and Set elements (these objects must not change state while in the collection)
- Immutable Objects are often also ValueObjects, e.g. when used as Map keys
Make a class immutable by following these guidelines :
- always put all data in the constructor, instead of using a no-argument constructor combined with subsequent calls to setXXX methods
- do not provide any methods which can change the state of the object in any way - not just setXXX methods, but any method which can change state
- ensure no methods can be overridden - make the class final, or use static factories and keep constructors private
- make fields final
- if a mutable object field's state is "owned" by the native class, such that no other class is to be allowed to change the state of the field, then when the field "crosses the interface" (as in a get method, when the field is returned to the user, or in the constructor itself), then a defensive copy must be made, in order to maintain encapsulation.
- if a mutable object field's state is not "owned" by the native class, then defensive copies of the object field are not necessary
Example
import java.util.Date;
/**
* Planet is an immutable class, since there is no way to change
* its state after construction.
*/
public final class Planet {
/**
* Primitive data is always immutable.
*/
private final double mass;
/**
* An immutable object field. (String objects never change state.)
*/
private final String name;
/**
* 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 dateOfDiscovery;
/** Sole constructor */
public Planet (double mass, String name, Date dateOfDiscovery) {
this.mass = mass;
this.name = name;
//make a private copy of dateOfDiscovery
//this is the only way to keep the dateOfDiscovery
//field private, and shields this class from any changes
//to the original dateOfDiscovery object
this.dateOfDiscovery = new Date(dateOfDiscovery.getTime());
}
//gets but no sets, and no methods which change state
public double getMass() {
return mass;
}
public String getName() {
return name;
}
/**
* 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.
*/
public Date getDateOfDiscovery() {
return new Date(dateOfDiscovery.getTime());
}
}
-- JohnOHanley - 19 Jul 2003
Also see
(An editor apparently disagrees strongly with PrivateMembersLast...)
-- JohnOHanley - 23 Jul 2003
----- Revision r6 - 20 Jul 2004 - 08:16:45 - Main.hlovatt
|