I'm developing a Java application that will be executed in more than one computer. I'm using the observer pattern to make changes visible and Hibernate to persist entities. The observer pattern works fine if I create several instances of my Observer (by clicking a button) and changes are reflected in every window. But if I create several instances of my Observer by executing .jar file, the observer pattern doesn't work. I think this is alike executing the application in various computers.
I'd like to know how I can make changes visible in a remote computer.
Here's my Server class
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Observable;
public class Server extends Observable{
private ArrayList clients = new ArrayList();
public Server() {
}
public void addClient(String name){
this.clients.add(new Client(name, this));
this.setChanged();
this.notifyObservers();
}
public ArrayList getClients() {
return clients;
}
public void setClients(ArrayList clients) {
this.clients = clients;
}
public void sendMessage(Message message){
Iterator iter = this.clients.iterator();
while(iter.hasNext()){
Client client = (Client)iter.next();
if (message.getSender()!= client){
client.recibirMensaje(message);
}
}
}
public Client searchClient(String name){
Client auxClient = null;
boolean state = false;
Iterator iter = this.clients.iterator();
while (iter.hasNext() && state == false){
Client unCliente = (Client)iter.next();
if (unCliente.getName().equals(name)){
auxClient = unCliente;
state = true;
}
}
return auxClient;
}
}
Here's my Client class
import java.util.ArrayList;
import java.util.Observable;
public class Client extends Observable {
private String name;
private ArrayList messages = new ArrayList();
private Server server = new Server();
public Client() {
}
public Client(String name) {
this.name = name;
}
public Client(String name, Server server) {
this.name = name;
this.server = server;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList getMessages() {
return messages;
}
public void setMessages(ArrayList messages) {
this.messages = messages;
}
public Server getServer() {
return server;
}
public void setServer(Server server) {
this.server = server;
}
// Enviar mensaje al servidor
public void enviarMensaje(String msg){
this.server.sendMessage(new Message(msg, this));
}
// Enviar mensaje a un solo destinatario
public void enviarMensaje(String msg, Client unDestinatario){
unDestinatario.recibirMensaje(new Message(msg, this, unDestinatario));
}
public void recieveMessage(Message message){
this.messages.add(message);
this.setChanged();
this.notifyObservers();
}
@Override
public String toString() {
return name;
}
}
Here's my Message class
import java.util.Observable;
public class Message extends Observable {
private String msg;
private Client reciver;
private Client sender;
public Message() {
}
public Message(String msg, Client sender) {
this.msg = msg;
this.sender = sender;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Client getReciver() {
return reciver;
}
public void setReciver(Client reciver) {
this.reciver = reciver;
}
public Client getSender() {
return sender;
}
public void setSender(Client sender) {
this.sender = sender;
}
public Message(String msg, Client sender, Client reciver) {
this.msg = msg;
this.reciver = reciver;
this.sender = sender;
this.reciver = reciver;
}
@Override
public String toString() {
return sender.getName() + ": " + msg;
}
}
The observers are two JFrame that show a list of objects (Clients ans Messages) when one of them is added from the same JFrame.
Alberto.