Mediator
What is it?
Mediator is a behavioral design pattern that manages communication between objects through a central intermediary instead of letting them talk to each other directly. Its main purpose is to prevent classes from becoming tightly connected to one another.
Normally, one object directly calls another object. As the project grows, this can create too many references, complicated dependencies, and code flow that becomes hard to follow. With Mediator, objects do not need to know each other; they only know the mediator. Communication happens through this central structure.
In Unity, this is often seen through systems like UIMediator, CombatMediator, GameFlowMediator, UIManager, or carefully designed manager classes. For example, when an enemy dies, the Enemy class does not directly call the score system, audio system, UI system, and achievement system. Instead, it notifies the mediator. The mediator then decides which systems should be updated and what actions should happen. This keeps the Enemy class independent from the details of other systems.
When is it used?
Mediator is especially useful when one action requires coordination between multiple systems. For example, when the player takes damage, the mediator can coordinate updating the health bar, playing a sound effect, shaking the camera, and notifying the achievement system. Connecting all of these systems directly to the Player class would make the code messy. Using a mediator keeps the communication more organized.
It is also useful in UI systems. If buttons, panels, sliders, and popups affect each other, it is cleaner to manage them through a UIMediator instead of making each panel directly reference the others.
It can be used when:
- Multiple systems need to be coordinated
- You want to reduce direct references between objects
- You want to separate systems like UI, gameplay, audio, score, or achievements
- Communication logic should be controlled from one place
- You want the code to be easier to test and extend
However, it should not be used everywhere. If there is a simple, fixed, one-to-one relationship between two classes, Mediator may create an unnecessary extra layer. For example, a direct relationship between Player and PlayerInventory is often easier to understand.
Animation
Code
using UnityEngine;
namespace Patterns.Behavioral.Mediator
{
public interface IGameMediator
{
void NotifyEnemyKilled(Enemy enemy, int scoreValue);
void NotifyPlayerDamaged(int damage);
void NotifyGameOver();
}
}Advantages and Disadvantages
• Advantages:
- It reduces dependencies because objects do not directly know each other.
- Changing or adding behavior becomes easier.
- Communication logic is gathered in one controlled place.
- It also makes testing easier because classes are less dependent on other systems.
• Disadvantages:
- It can add unnecessary abstraction in simple cases.
- Debugging can be harder because the flow passes through one central place.
- The mediator can become too large if responsibilities are not separated.
- If too many unrelated systems are handled by the same mediator, readability decreases.
- It may hide dependencies if the project structure is not clear.
So the key point is balance. If many systems really need to be coordinated, Mediator is useful. If there is only a simple call between two classes, a direct method call may be cleaner.
Tips
One of the most practical ways to use Mediator in Unity is to create mediators based on responsibility. For example, UI interactions can be handled by a UIMediator, combat-related communication by a CombatMediator, and level flow by a GameFlowMediator. This prevents one central class from controlling the entire project.
If you use a GameManager, be careful not to turn it into a class that does everything. A GameManager can manage the general game flow, but UI, combat, audio, and input logic should be separated when the project grows. For example, using CombatMediator, UIMediator, and AudioMediator can keep the project cleaner.
- Manage UI panel opening and closing through a
UIMediatorinstead of directly connecting panels to each other. - Keep gameplay classes like
Enemy,Player, orNPCindependent from UI and audio systems. - Use mediators for coordination, not for storing every piece of game logic.
- Keep mediator responsibilities focused and clear.
- Avoid putting all communication into a single
GameManager. - If the mediator starts getting too large, split it by responsibility.
In short, Mediator is a strong pattern for separating systems and making a Unity project more flexible. When used in the right place, it makes the code cleaner. When used unnecessarily, it can add extra complexity.