vendredi 29 mai 2020

Can someone explain the code using the Mediator pattern or Bridge pattern?

Can someone explain the following code using the Mediator pattern or Bridge pattern?

public interface Relay {
  public void send(String m, User u);
}

...

public abstract User{
  private Relay relay;
  public User(Relay r) {
    relay = r;
  }

  public void send(String message) {
    relay.send(message, this);
  }
...
  public abstract void receive(String message);
}

...

public class ApplicationRelay implements Relay {
  private ArrayList<User> users;
  public ApplicationRelay() {
    users = new ArrayList<User>();
  }

  ...

  public void send(String m, User originator) {
    for(User user: users) {
      if(user != originator) {
        user.receive(m);
      }
    }
  }
}

  1. As for my view, the reason for using the Bridge pattern is: The Bridge pattern was designed in the code, which is one of kinds of structural pattern. Because, it decouples the functional abstraction from the implementation so that the two are independently. In the case, the interface Relay acts as the bridge for the abstract User. The class ApplicationRelay implements relay and it’s independent of the functional abstraction. I'm not sure it's correct for using the bridge pattern.Can some one give me some answer?

  2. I don't really understand how use the mediator pattern in the code. The defination of mediator pattern is that the mediator pattern uses a mediation object to encapsulate the interaction of a series of objects to avoid objects explicitly refer to each other and reduce the coupling between objects . However, in this case, I cannot find the madiator in the codes and it seems that no interaction between the objects. Is the madiator is the ApplicationRelay? Can someone gives me some explaination?

Aucun commentaire:

Enregistrer un commentaire