Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Immutability and Singleton solves the same purpose?

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" ?

like image 513
Rahul Shivsharan Avatar asked Dec 24 '22 03:12

Rahul Shivsharan


1 Answers

Not really.

Immutable class also doesn't mean you cannot create more objects of the same immutable class. For singleton, usually one instance per JVM is necessary for it to be called singleton.

Immutable means the value once assigned cannot be changed, so usually you won't have setter methods in immutable, but no such requirement is there for singleton. But a singleton is more from memory perspective, only one instance will live. You cannot change the instance , but you can use the singleton to hold whatever value you want and you can change it at any point in time as well.

e.g. we used singleton class as a service locator. We could change the services at any given point in time based on our need. For the same thing, I can create 2 objects of immutable class with same variables, but holding different values.

Hope this helps.

like image 96
Rishi Goel Avatar answered Dec 26 '22 17:12

Rishi Goel