I am socket-programming a chat client-server in Java. I need to share variables between clients. I tried to use Singleton Design Pattern, but it I get a different instance of the Singleton class every time I run a new instance of the client class.
Here is my code. I need to share clients
between clients, so that each one knows all the others.
private LinkedHashMap<String, String> clients = new LinkedHashMap<String, String>();
private static ClientListModel instance = null;
private ClientListModel() {
System.out.println("NEW INSTANCE!");
}
public static ClientListModel getInstance() {
if (instance == null)
instance = new ClientListModel();
return instance;
}
"NEW INSTANCE!" is being printed out every time I run my client code, and in effect I get an empty clients
LinkedHashMap.
What is the proper way to share data between two or more running instances of the same class?
Thank you!