Mediator Class

Stephen mentioned the mediator pattern in the lecture. I’ve not come across this before. From a quick google, it sounds like this allows classes to communicate with other classes (or even other instances of itself) without knowing about them - kinda like an interface. I think in the case of this solution, a mediator is overkill, but I can definitely see a use if you wanted to access multiple things from one event, say making all your grenades explode as soon as one does.

Anyhoo, I created a GrenadeAudioMediator.cs:

public static class GrenadeAudioMediator
{
    public static void GrenadeBeep()
    {
        AudioManager.Instance.Grenade_OnGrenadeFlash();
    }

    public static void GrenadeExplode()
    {
        AudioManager.Instance.Grenade_OnGrenadeExplode();
    }
}

And then, in Grenade:

    private void Explosion()
    {
        Instantiate(_explosionVFX, transform.position, Quaternion.identity);
        GrenadeAudioMediator.GrenadeExplode();
        Destroy(gameObject);
    }

As ever, I guess practice will help me work out WHEN to use these patterns

:slight_smile:

Privacy & Terms