Tuesday, December 5, 2017

Immutable Objects

Immutability: - In object oriented programming language, if an object is created once, it's content can't be changed or updated. Java has many built in classes with immutable capability. String class is heavily used immutable class in Java programming. Whenever any String object is created, we can't modify or change it's content. Every method available in String class, that seems to modify a String object actually creates new object and returns new object with modified content.

i.g.

package com.kd.example.immutable;

public class StringTest {

    public static void main(String[] args) {
        String s1 = "hello string";
        String s2 = s1.toUpperCase();
    }
}

In above example, toUpperCase() is updating content of s1 to upper case. toUpperCase() is not actually updating s1's content but creating new String object with upper case content and returning it to s2. 


Create Immutable Object: - 
To create an immutable object you should take care of few things:
  • Final Class
  • No setter, only getter
  • Only Constructor to modify content
  • If your class has any mutable reference, create a clone or new instance while returning through getter
i.g. Lets create an immutable class ImmutableClass.java:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

final public class ImmutableClass {

       public ImmutableClass(String name, int age, Address address, List<String> phones) {
             this.name = name;
             this.age = age;
             this.address = address;
             this.phones = phones;
       }

       private String name;
       private int age;
       private Address address;
       private List<String> phones;

       public String getName() {
             return name;
       }

       public int getAge() {
             return age;
       }

       public Address getAddress() {
             return createCopy(address);
       }

       private Address createCopy(Address address2) {
             Address newAddress = new Address();
             newAddress.setCity(address.getCity());
             newAddress.setLine1(address.getLine1());
             newAddress.setLine2(address.getLine2());
             newAddress.setState(address.getState());
             newAddress.setZipcode(address.getZipcode());
             return newAddress;
       }

       public List<String> getPhones() {
             return createCopy(phones);
       }

       private List<String> createCopy(List<String> phones) {
             List<String> newPhones = new ArrayList<>();
             Collections.copy(newPhones, phones);
             return newPhones;
       }
}
 


public class Address {
       private String line1;
       private String line2;
       private String city;
       private String state;
       private int zipcode;

       public String getLine1() {
             return line1;
       }

       public void setLine1(String line1) {
             this.line1 = line1;
       }

       public String getLine2() {
             return line2;
       }

       public void setLine2(String line2) {
             this.line2 = line2;
       }

       public String getCity() {
             return city;
       }

       public void setCity(String city) {
             this.city = city;
       }

       public String getState() {
             return state;
       }

       public void setState(String state) {
             this.state = state;
       }

       public int getZipcode() {
             return zipcode;
       }

       public void setZipcode(int zipcode) {
             this.zipcode = zipcode;
       }
}

In above example, you can see, ImmutableClass.java has an instance of Address class which is Mutable and phones as a List which is also mutable. There might be some more way to refer mutable classes in your immutable class. But the basic idea to handle mutability in immutable class is same as applied in ImmutableClass.java. Refer this implementation and design your immutable class as per your need.
If you have any query or any suggestion, feel free to comment in comment box.

0 comments:

Post a Comment