mardi 29 mars 2016

Is this Adapter or Facade?

So I've convinced myself I was using Facade, but after taking a look at the Adapter pattern, I feel confused right now. So basically, I have 3 classes involved, a ServerCommunication, an Anti-Spam Server and a Normal Server. Both servers use the ServerCommunication to start working. I created a class where I created a method to start one server type or another, plus I created some other method like "printOnlineUsers" where I get the list of clients connected from the server and print them , and some other methods. It's basically a class where you can manage a Server easily. I don't know if these details are enough, so here's a piece of my code.

public class EzServer {

private ServerComm serverComm = new NetServerComm();
private Server server = Server.getInstance();
private ServerComm decoratedServerComm = new TransparentSpamFilter(serverComm);

public void startNormalServer() {
    server.start(serverComm);
    JOptionPane.showMessageDialog(null, "Normal server");
    serverComm.start();
}

public void startAntiSpamServer() {
    JOptionPane.showMessageDialog(null, "Anti spam server");
    server.start(decoratedServerComm);
    decoratedServerComm.start();
}

public int getTotalOnlineUsers() {
    return Server.getInstance().getConnectedClients().size();
}

public boolean isClientOnline(String nickname) {
    return Server.getInstance().getConnectedClientByNickname(nickname) != null;
}

public void disconnectClient(String nickname) {
    serverComm.disconnectClient(nickname);
}

public void printOnlineUsers() {
    List<Client> list = Server.getInstance().getConnectedClients();
    if (list != null && !list.isEmpty()) {
        System.out.println("Online users:");
        for (Client client : list) {
            System.out.println(client.getNickname());
        }
    } else {
        System.out.println("No online users");
    }
}
}

Aucun commentaire:

Enregistrer un commentaire