'Immutability' means something which is not capable of OR susceptible to change.
An object whose state can not be changed after its creation is known as immutable object. A class which provide implementation for such object is known as immutable class.
An object state is comprised of its given attributes and their current values. For immutable objects, no attribute can be changed after its creation. Immutable objects are created, used and destroyed and they can never be altered like mutable objects.
Examples of immutable classes : java.lang.String , wrapper classes for the primitive types: java.lang.Integer, java.lang.Byte, java.lang.Character, java.lang.Short, java.lang.Boolean, java.lang.Long, java.lang.Double, java.lang.Float,java.io.File etc
Define immutable class
Following things needs to be taken care of while creating or converting existing mutable class to immutable class.
1. Make all fields private(to avoid direct modification to attribute from outside of class) and final(to avoid modification in attribute value once some value is assigned initially)
2. Remove all those methods which sets/modifies value of any object attribute.
3. Either declare class as 'final' or make all constructors private and provide static factory method to create object. By this, we will avoid possible extension and method overriding which might allow change in object state.
4. Initialize all required fields in constructor.
5. In case if you are using collections, then make sure you are using Collections.unmodifiable---- (). It should also contain immutable objects only.
5. In case if you are using collections, then make sure you are using Collections.unmodifiable---- (). It should also contain immutable objects only.
6. Composite Objects :
Composite objects(i.e. Objects which are being used as attribute inside class which we are making as immutable), needs special treatment as simple getter method for such a composite object will return reference to these composite objects outside your class and it's state can be easily modified from outside of class.
Following steps needs to be taken for composite objects
Following steps needs to be taken for composite objects
A. Either make your composite object as 'immutable' by following all above process for composite object class OR
B. While assigning or returning value to such composite object, always use copy from input object or return copy of actual object respectively.
Advantages
1. Thread Safe : Thread safety is required when object state can be compromised due to parallel access by multiple threads.Immutable object can't be modified once its created, hence its considered as thread safe.
2. Object Sharing/Pooling : Immutable objects can be shared from multiple places and it can be reused until destroyed as it's state can not be changed after it's creation. Immutable objects can also be used for object pooling purpose.
3. Performance improvement : As cost of object creation can be saved when we need same object created multiple times in application.
4. Key in hashing collection : Hashing collection uses hashcode() and equals() methods on 'key' object. These methods are normally based on attributes of object, which you don't want to change after insertion of object in hashing collection to avoid different hashcode value during insertion and while retrieval. Hence key objects of hashing collection should be immutable to avoid any change.
3. Performance improvement : As cost of object creation can be saved when we need same object created multiple times in application.
4. Key in hashing collection : Hashing collection uses hashcode() and equals() methods on 'key' object. These methods are normally based on attributes of object, which you don't want to change after insertion of object in hashing collection to avoid different hashcode value during insertion and while retrieval. Hence key objects of hashing collection should be immutable to avoid any change.
Disadvantages
1. Overhead of maintaining lot of objects : Immutable objects can not be changed, hence when application require even smaller change in object, we are bound to create new object.
Very well articulated ! the easy English helps to under the concept quickly.
ReplyDeleteKeeping writing.
Thank you Makarand :)
Delete