Benefits of making a class immutable
Lets first identify benefits of making a class immutable. Immutable classes are
- 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
- allow hashCode to use lazy initialization, and to cache its return value
- 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)
- have their class invariant established once upon construction, and it never needs to be checked again
- always have “failure atomicity” (a term used by Joshua Bloch) : if an immutable object throws an exception, it’s never left in an undesirable or indeterminate state
1) Don’t provide “setter” methods — methods that modify fields or objects referred to by fields.
This principle says that for all mutable properties in your class, do not provide setter methods. Setter methods are meant to change the state of object and this is what we want to prevent here.
2) Make all fields final and private
This is another way to increase immutability. Fields declared private will not be accessible outside the class and making them final will ensure the even accidentally you can not change them.
3) Don’t allow subclasses to override methods
The simplest way to do this is to declare the class as final. Final classes in java can not be overridden.
4) Special attention when having mutable instance variables
Always remember that your instance variables will be either mutable or immutable. Identify them and return new objects with copied content for all mutable objects. Immutable variables can be returned safely without extra effort.
A more sophisticated approach is to make the constructor private and construct instances in factory methods.
public class MyImmutable { private final Integer a; private final String s; private final Demo ss; private MyImmutable(int a1, String s1, Demo ss1) { this.a = a1; this.s = s1; this.ss = new Demo(ss1.getAge()); } // factory method for obj creation public static MyImmutable newInstance(int a1, String s1, Demo ss1) { return new MyImmutable(a1, s1, ss1); } public Integer getA() { return a; } public String getS() { return s; } public Demo getSs() { return new Demo(ss.getAge()); } @Override public String toString() { return "MyImmutable [a=" + a + ", s=" + s + ", ss=" + ss + "]"; }}package com.cic.testcase;public class Demo { public int age; Demo(int s) { this.age = s; } int getAge() { return age; } @Override public String toString() { return "Demo [age=" + age + "]"; } }public class Test { public static void main(String[] args){ MyImmutable im = MyImmutable.newInstance(14, "age",new Demo(20)); System.out.println(im); modification(im.getA(), im.getS(), im.getSs()); System.out.println(im); } private static void modification(int a2, String s2, Demo ss2){ a2 = 20; ss2.age = 11; s2 = "changed"; } } |
OUTPUT:
MyImmutable [a=14, s=age, ss=Demo [age=20]]
MyImmutable [a=14, s=age, ss=Demo [age=20]]
MyImmutable [a=14, s=age, ss=Demo [age=20]]
MyImmutable [a=14, s=age, ss=Demo [age=20]]
Comments
Post a Comment