Rahul Shivsharan Rahul Shivsharan - 4 months ago 8
Javascript Question

Does Immutability and Singleton solves the same perpose?

What I have read is Immutability means the value once assigned remains constant, i.e. once assigned, it is reflected as the same value across the code.

Mostly Immutability is using in Functional Programming paradigm in multi threaded environment.

But.

Does singleton pattern also solves the same purpose,

Please find below of singleton written in java,

package com.main;

class Student{
private Student(){

}
public static Student INSTANCE;
static{
INSTANCE = new Student();
}

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString(){
return "Student is "+this.name;
}
}


public class SingletonTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student student = Student.INSTANCE;
student.setName("Arnold");

student = Student.INSTANCE;
student.setName("Kevin");

student = Student.INSTANCE;
student.setName("Jim");

student = Student.INSTANCE;
student.setName("Falcon");

student = Student.INSTANCE;
student.setName("Sarvik");


student = Student.INSTANCE;
System.out.println(student);

}

}


The output of the above code is

Student is Sarvik


In an same way, I have written singleton in javaScript as well,

as follows

var Student = (function(){
var name = "";
var obj = {
setName : function(studentName){
name = studentName;
},
getName : function(){
return name;
}
}

return {
INSTANCE : obj
}
})();


var student = Student.INSTANCE;
student.setName("Arnold")

student = Student.INSTANCE;
student.setName("Kevin");

student = Student.INSTANCE;
student.setName("Jim");

student = Student.INSTANCE;
student.setName("Falcon");

student = Student.INSTANCE;
student.setName("Sarvik");

student = Student.INSTANCE;

console.log("Student is "+student.getName());


the output is as follows

rahul@rahul:~/myPractise/JavaScript-DesignPatterns$ node singleton.js
Student is Sarvik
rahul@rahul:~/myPractise/JavaScript-DesignPatterns$


In both (i.e. javaScript and java) implementation of Singleton pattern
you can see that the state of object remains same, it doesn't change.

Even if we reset the value of object,
the output gives the latest reset value.

So does using Singleton pattern I attained Immutability in my code of object "Student" ?

Answer

No: singleton-ness and immutability are orthogonal. A singleton can be mutable or immutable; a non-singleton can be mutable or immutable.

However, a singleton must be thread-safe if used in multiple threads; immutable objects are inherently thread-safe.


Your Student class is approximately singleton, but not immutable: any class where you have a setter method that mutates a member variable cannot be immutable.

However, your Student class is not thread-safe, or even safe:

  • You can mutate the value of name, but there is no guarantee about the visibility of the name field in other threads: because you don't synchronize access to the variable (or use an AtomicReference, or volatile), it is possible that one thread can update the name, but other threads continue to see the old value. As such, you might observe thread interference effects.
  • It is possible for users of the class to null out the INSTANCE field, meaning that anybody who attempts to access the value of the field at any time must first check if it is null. This is easy to fix by making the field final; but it's better simply to make the class into a single-element enum.
Comments