Although the example shows a very simple Mediator, it is possible that
the mediator can have very complex logic. For example, messages can only be sent between colleagues of the same department.
ComputerColleague class can maintain a private reference to the NetworkMediator and thus register or unregister itself for notification. Let’s see the code example,
The Mediator pattern makes provisions for more than one mediator. For example,
there may be many different departments in a company. Each department
may have a different moderator, different rules of engagement, and a different list of
users, but the structure of the lists is identical. Therefore, creating a new Mediator is
merely an instantiation operation and does not require subclassing or an interface.
Where To Apply Mediator Pattern?
When a set of objects communicate in well-defined but complex ways. The resulting
interdependencies are unstructured and difficult to understand.
When reusing an object is difficult because it refers to and communicates with
many other objects.
When a behavior that’s distributed between several classes should
be customizable without a lot of subclassing.
Observer Pattern C# - The observer design pattern enables a subscriber to register with and receive notifications from a provider. It is suitable for any scenario that requires push-based notification. The pattern defines a provider (also known as a subject or an observable) and zero, one, or more observers.
Publish Subscribe Design Pattern In C# - Publish Subscribe or Pub-Sub is a design pattern that allows loose coupling between the application components. This post explains the implementation detail of Pub-Sub using Delegates, EventHandlers and Event keyword in C#.
Using the event aggregator pattern to communicate between view models by Magnus Montin - In this post, Magnus explains how by introducing an event aggregator in between the publishers and subscribers, you can remove tight coupling between them. The subscriber observes the event aggregator instead of the publisher and the publisher knows only about the event aggregator and not about the subscribers. This pattern is much similar to the mediator pattern.
Subscribe to Code with Shadman
Get the latest posts delivered right to your inbox