简体   繁体   中英

mediator design pattern implemented in java

is this correct way to implement Mediator design pattern in java ?

public class chat {
    public static void showMesg(Color c , String msg){
    System.out.println(new Date().toString() +" "+c + " " + msg +" ");

    }
}

then i call the "ShowMesg" method in method Draw of red class

public class Red implements Color {
      public void draw() {
          chat.showMesg(this, "Hey this is Red :D");
      System.out.println("Red color");
      }

No. You did not implement Mediator pattern (courtesy:dzone article by James Sugrue ) properly.

Structure:

在此处输入图片说明

The Mediator defines the interface for communication between Colleague objects.

The ConcreteMediator implements the Mediator interface and coordinates communication between Colleague objects.

It is aware of all the Colleagues and their purpose with regards to inter communication.The ConcreteColleague communicates with other colleagues through the Mediator .

Your example does not use Mediator pattern at all. You have tight coupling between your objects.

Have a look at code example in below post for better understanding:

Mediator Vs Observer Object-Oriented Design Patterns

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM