What is the need for Mediator Pattern in C#?

Mediator pattern concentrates about how you establish interaction between objects. Generally objects interact by passing messages to one another directly.



This enforces strong coupling between objects. To reduce this coupling and to have a mediator class to mediate between two objects, you implement mediator pattern. Here is a sample code of mediator class:

public class mediatorClass {
private derivedClass1 obj1;
private derivedClass2 obj2;
public derivedClass1 Obj1 {
set { obj1 = value; }
}
public derivedClass2 Obj2 {
set { obj2 = value; }
}
public void sendMessage(string msg, baseClass obj) {
if(obj == obj1) {
obj2.notifyCommunication(msg);
}
else if(obj == obj2) {
obj1.notifyCommunication(msg);
}
}
}

This is just a partial code snippet. Your program should also include an abstract class called baseClass that contains mediatorClass instance as its member. This baseClass is inherited by two classes namely derivedClass1 and derivedClass2. In mediatorClass, you create instances of these two derived classes and achieve communication among them using the sendMessage method. Note that your derived classes should implement the method notifyCommunication.

| What is the need for Bridge Pattern in C#? | What is the need for Builder Pattern in C#? | What is the need for Chain of Responsibility Pattern in C#? | What is the need for Command Pattern in C#? | What is the need for Composite Pattern in C#? | What is the need for Decorator Pattern in C#? | What is the need for Flyweight Pattern in C#? | What is the need for Interpreter Pattern in C#? | What is the need for Iterator Pattern in C#? | What is the need for Mediator Pattern in C#? | What is the need for Memento Pattern in C#? | What is the need for Prototype Pattern in C#? | What is the need for State Pattern in C#? | What is the need for Strategy Pattern in C#? | What is the need for Template Method Pattern in C#? | What is the need for unsafe code in C#? | What is the purpose of assert() in C#? | What is the purpose of AutoResetEvent in .NET? | What is the purpose of Console.ReadLine() in C#? | What is the purpose of machine.config file in .NET? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.