Sitemap
Prince Jha

Prince Jha

Software Developer, Computer Science Engineer, Programmer, Gamer.

Immutability

2 min readJan 18, 2023

--

[[Design Patterns — 4]]

Concept of Immutable objects:

The concept of Immutability in Design is that we only allow objects whose fields are read only. The user of an immutable object should not be able to change any attribute on the object. If the attributes of immutable object are primitive values then they must be final and thus assignment operation should be disallowed preventing modification to the primitive values. If the attributes of immutable object are references to other object then the referent object must be an immutable object. Note the recursive nature of the definition of immutability.

Characteristics of Immutable Design:

  • Design objects that never need to change their state
  • Allowing several sources of modification to an object’s state which can lead to bugs. In a multi threaded application synchronization may be required to avoid inconsistent state of the object.
  • Instead of allowing modification to an object’s state, we can create a new object altogether for the use case. We can ensure each time a different state of object is necessary, a new object will be allocated memory. This may lead to increase in memory utilisation but will ensure that programmer always uses an immutable object.
  • Using immutable objects can be beneficial in a multi threaded application since the design guarantees that an object’s state can never be corrupted.
  • The Design has a limitation that the size of objects should be small. Creating new objects for large objects causes memory wastage as well as runtime inefficiency.

Get Prince Jha’s stories in your inbox

Join Medium for free to get updates from this writer.

Class Design Diagram for the Immutable Design:

Press enter or click to view image in full size

ImmutableClass: Class in this role define the design of an immutable object.

  • The primitive fields of an immutable object are effectively final
  • The associated Java reference is also effectively final.
  • The associated referent object is an immutable object.
  • Notice the lack of setters and read only nature of the attributes.

Example Code to illustrate the use of Immutable Design:

class Complex {
// z = a + ib
private final double a;
private final double b;

public Complex(double x, double y) {
this.a = x;
this.b = y;
}

public final double getRealPart() {
return a;
}

public final double getImaginaryPart() {
return b;
}

public final Complex add(Complex other) {
return new Complex(this.a + other.a, this.b + other.b);
}

public final Complex subtract(Complex other) {
Complex negativeOneComplex = new Complex(-1, 0); // z = -1 + i(0)
Complex negativeOther = other.multiply(negativeOneComplex);
return this.add(negativeOther);
}

public final Complex multiply(Complex other) {
double realPart = (this.a * other.a) - (this.b * other.b);
double complexPart = (this.a * other.b) + (this.b * other.a);
return new Complex(realPart, complexPart);
}
}

The String Class in Java is a typical example of an immutable class.

--

--

Prince Jha
Prince Jha

Written by Prince Jha

Software Developer, Computer Science Engineer, Programmer, Gamer.