Looking to figure something out about the singleton pattern.
If I implement a singleton pattern like the below, what could I do so that other classes could update and see updates to fields someString and someInt?
From what I read about the Singleton pattern, immutability was not one of the prerequisites. So technically could I have setter methods for the fields and change them and have these changes visible to other classes? For example if I have another two classes implementing Runnable and printing Foo's fields every few seconds. I tried this and what happened was that each class sees its own updates only and none of the other classes'.
//generic Singleton implementation
public class Foo {
private static Foo instance;
private String someString;
private int someInt;
private Foo(){
someString = "a";
someNum = "1";
}
public static Foo getInstance(){
if(instance == null){
instance = new Foo();
}
return instance;
}
Aucun commentaire:
Enregistrer un commentaire