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:
- Don't provide any methods that modify the object's state
- Ensure that theclass can't be extended.
- Make all fields final.
- Make all fields private.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
- 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
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.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);
No comments:
Post a Comment