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 .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" ?
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 .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" ?
Share Improve this question edited Aug 31, 2016 at 7:25 Sudipta Roy 1031 gold badge3 silver badges11 bronze badges asked Jul 27, 2016 at 7:13 Rahul ShivsharanRahul Shivsharan 2,5618 gold badges40 silver badges60 bronze badges 3- 1 stackoverflow./questions/13784244/… – Unknown Commented Jul 27, 2016 at 7:15
- 1 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. – Andy Turner Commented Jul 27, 2016 at 7:22
-
2
Your
Student
class is clearly not immutable: you have a setter method. – Andy Turner Commented Jul 27, 2016 at 7:27
4 Answers
Reset to default 8Not 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.
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 thename
field in other threads: because you don't synchronize access to the variable (or use anAtomicReference
, orvolatile
), 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 fieldfinal
; but it's better simply to make the class into a single-element enum.
First of all, the singleton example you have given, is not actually a singleton.
The singleton patterns force you to use only one object per class throughout the application. In your example, one can create as many objects as possible.
Forcing single object creation can be achieved using private constructor and providing 'INSTANCE' every time whenever request for the object es to that class using some static method say getInstance().
Immutability has different purpose. Its mostly related to the security of an object, e.g. once object is created you can not change its state.
Its obvious you have to provide the state while creating that object. I mean you need a parametric constructor to assign a permanent state to that object.
So basic difference is:
Singleton: You can not have more that one object at any case.(unlike immutable object , you are not restricted to state change but mostly you are not suppose to change singleton object's state)
Immutability: You can not change the state of object at any case, but you can have as many object as heap permits ;-)
You student class is all but immutable. The public setName() of the Student class prevents that states be immutable. An immutable object provides no way to change its state.
if your object was immutable, your student instance Student.INSTANCE
could not change its name. But, you changes its name several times. And your systemOut
at the end confirms the mutability : the last name "setted" is displayed by the toString()
method.
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);
Besides, if an immutable object want to provide operations related to state modifications, it will not apply to the instance itself, it returns rather a new instance of object with this new state.