What is Immutable Object?
The immutable object is the object whose state cannot be changed, but immutable object values often required modification and we are not here to declare constants so whenever immutable object values get modified it create a new object with new value.
We need immutable object as a cache key. As in no circumstances we are not allowed to change the cache key.
Here we will see how to create an immutable object and what are the advantages and disadvantages of it.
A class needs to meet certain conditions to become immutable object when we create an object of it. We will also see why this condition needs to be meet in order to get the immutable object.
The immutable object is the object whose state cannot be changed, but immutable object values often required modification and we are not here to declare constants so whenever immutable object values get modified it create a new object with new value.
We need immutable object as a cache key. As in no circumstances we are not allowed to change the cache key.
Here we will see how to create an immutable object and what are the advantages and disadvantages of it.
A class needs to meet certain conditions to become immutable object when we create an object of it. We will also see why this condition needs to be meet in order to get the immutable object.
- The class must be declared final. Why? So that no one can extend it.
- All its fields must be final and private. Why? So that no can access from outside of this class and once the value is set then no one can change it.
- Don't allow "setter" methods in order to protect any change. Why? No one can change the value from outside.
- Any field which is a type of mutable (value can be changed) object should not directly return the reference. Otherwise, caller method can modify the value of it.
- Any method which modifies any field which is a type of mutable object should be private.
- The class may have factory method instead of the constructor as it will provide more sophistication.
- In some of the cases where outside code needs to send mutable object to immutable object here things become more tricky. In this case, we need to create a defensive copy of that mutable parameter object and so that immutable class can work on a separate copy.
ImmutableClass.java
- import java.util.*;
- import java.util.concurrent.*;
- public class ImmutableClass {
- private final Date callDate;
- private final String friendName;
- private final List<String> address = new CopyOnWriteArrayList<String>();
- public ImmutableClass(Date date, String name, List<String> modifyData){
- callDate = new Date(date.getTime());
- friendName = name;
- Collections.copy(address,modifyData);
- if(callDate.compareTo(new Date()) == 1) {
- throw new IllegalArgumentException("Date cannot be greater than today's date. ");
- }
- modifyAddress();
- }
- public Date modifyDate(Date date){
- return new Date(date.getTime());
- }
- private void modifyAddress(){
- address.add(this.friendName);
- }
- public List<String> getAddress() {
- return Collections.unmodifiableList(address);
- }
- }
No comments:
Post a Comment