Reputation:
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");
}
Upvotes: 1
Views: 1005
Reputation: 38910
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
Upvotes: 3