Asdfasf

Saturday, August 23, 2014

EJ-15 Minimize Mutability

Ref: Effective Java by Joshua Bloch

An immutable class is simply a class whose instances can not be modified, all of the information contained in each instance is provided when it is created and is fixed for the lifetime of object.
There are many good reasons for this: Immutable classes are easier to design, implement and use than mutable classes. They are less prone to error and are more secure.
To make class immutable:
  1. Don't provide any methods that modify the object's state 
  2. Ensure that theclass can't be extended.
  3. Make all fields final.
  4. Make all fields private.
Here is a good sample of immutable class, Notice how the aritmetic operations create and return a new Complex instance rather than modifying this instance.

public final class Complex {
private final float re;
private final float im;
public Complex(float re, float im) {
this.re = re;
this.im = im;
}
// Accessors with no corresponding mutators
public float realPart() { return re; }
public float imaginaryPart() { return im; }
public Complex add(Complex c) {
return new Complex(re + c.re, im + c.im);
}
public Complex subtract(Complex c) {
return new Complex(re - c.re, im - c.im);
}
public Complex multiply(Complex c) {
return new Complex(re*c.re - im*c.im,
re*c.im + im*c.re);
}
public Complex divide(Complex c) {
float tmp = c.re*c.re + c.im*c.im;
return new Complex((re*c.re + im*c.im)/tmp,
(im*c.re - re*c.im)/tmp);
}
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Complex))
return false;
Complex c = (Complex)o;
return (Float.floatToIntBits(re) == // See page 33 to
Float.floatToIntBits(c.re)) && // find out why
(Float.floatToIntBits(im) == // floatToIntBits
Float.floatToIntBits(im)); // is used.
}
public int hashCode() {
int result = 17 + Float.floatToIntBits(re);
result = 37*result + Float.floatToIntBits(im);
return result;
}
public String toString() {
return "(" + re + " + " + im + "i)";
}
// Public constants - Page 66
public static final Complex ZERO = new Complex(0, 0);
public static final Complex ONE = new Complex(1, 0);
public static final Complex I = new Complex(0, 1);
public static void main(String args[]) {
Complex x = new Complex(2, 3);
Complex y = new Complex(2,-3);
System.out.println(x + " + " + y + " = " + x.add(y));
System.out.println(x + " - " + y + " = " + x.subtract(y));
System.out.println(x + " * " + y + " = " + x.multiply(y));
System.out.println(x + " / " + y + " = " + x.divide(y));
System.out.println(x.divide(y).multiply(y));
}
}
view raw Immutable.java hosted with ❤ by GitHub


  • Immutable objects are simple, an immutable object can be in exactly one state, the state in which it was created.
  • Immutable ojects are inherently thread-safe; they require no synchronization
  • Immutable ojects can be shared freely. One easy way to do this is to rpvoide public static final constants for frequently used values, for example
public static final Complex ZERO = new Complex(0, 0);
public static final Complex ONE = new Complex(1, 0);
public static final Complex I = new Complex(0, 1); 
 To summarize, resist the urge to write a setter method for every getter method. Classes should be immutable unless there's a very good reason to make them mutable.

No comments: