←All patterns
Structural

Facade

Give a tangle of subsystems one simple front door so the rest of the game only makes one call.

What Is It?

A Facade is a single class that offers a small, task-oriented API on top of several lower-level systems. It does not hide those systems from everyone; it just means ordinary callers no longer need to know how they fit together.

Think about what a convincing explosion needs: grab a free AudioSource from a pool, route it through the SFX group of an AudioMixer, briefly duck the music, and rumble the controller. Without a facade, every grenade, barrel and rocket script repeats that choreography. With an AudioFacade, they all call PlayExplosion(position) and move on.

The subsystems stay fully usable on their own. The facade simply bundles the common recipes, so the knowledge of "how we do an explosion here" lives in exactly one place.

When Is It Used?

Use it when many clients repeat the same multi-step sequence across several systems: audio and feedback, saving and loading, scene transitions, analytics plus achievements plus cloud sync.

It is a good boundary around a third-party package or a large internal module, so most of the codebase only touches a few well-named methods.

Avoid letting it grow into a god object that every system routes through. If the facade starts accumulating unrelated responsibilities, split it into several focused facades.

Interactive Demo

Click the controls and watch the objects collaborate. The console mirrors what the C# code below would log.

Facade Pattern
dependencies7calls written0
💣Grenade
🛢Barrel
🚀Rocket
AudioFacade
SourcePool
MixerRouter
MusicDucker
Haptics
———
Console
Press a button to see what happens…

Code

Assets / Scripts / Structural/ Facade ›Grenade.cs
using UnityEngine;

namespace Patterns.Structural.Facade
{
    /// <summary>
    /// Client. Knows nothing about pools, mixers or haptics: it asks the
    /// facade for an explosion and gets the whole package.
    /// </summary>
    public class Grenade : MonoBehaviour
    {
        [SerializeField] private AudioFacade audioFacade;
        [SerializeField] private float fuseSeconds = 2.5f;

        private float timer;

        private void OnEnable() => timer = fuseSeconds;

        private void Update()
        {
            timer -= Time.deltaTime;
            if (timer > 0f) return;

            audioFacade.PlayExplosion(transform.position);
            gameObject.SetActive(false);
        }
    }
}
3 files · namespace Patterns.Structural.FacadeC# · UTF-8 · LF

Before & After

Real-world smell: a grenade that drives every audio subsystem itself. Here is the code without the pattern, the refactored version, and the exact diff between them.

! What’s wrong with it?

Grenade holds the source pool, mixer router, music ducker and haptics. When it explodes it rents a source, routes it, plays the clip, ducks the music, rumbles the pad and runs its own Cleanup coroutine.

The rocket, the barrel and the mine repeat the same sequence, each with its own copy of the timings. Changing how explosions sound (another mixer group, shorter ducking) means editing every one of them, and each explosive needs all four subsystems set up in the Inspector.

✓ What changed

  • The four subsystem fields, explosionClip and the Cleanup coroutine are gone from the grenade; it holds a single AudioFacade reference.
  • The whole sequence becomes one intent-named call, audioFacade.PlayExplosion(transform.position).
  • Ordering, timings and returning the source to the pool now live in AudioFacade, so every explosive gets the same behaviour.
  • The exploded flag is gone too: the grenade can deactivate at once, because it no longer has to stay alive until the cleanup finishes.
Grenade.Before.cs → Grenade.After.cs+5/−37
1−using System.Collections;
21 using UnityEngine;
32
43 namespace Patterns.Structural.Facade
54 {
65 /// <summary>
7− /// Without Facade: the grenade drives the pool, mixer, music ducker
8− /// and haptics itself, in the right order, with its own timings.
6+ /// With Facade: the grenade asks for an explosion by intent. The facade
7+ /// owns the subsystems, the order and the cleanup.
98 /// </summary>
109 public class Grenade : MonoBehaviour
1110 {
12− [SerializeField] private AudioSourcePool pool = new();
13− [SerializeField] private MixerRouter mixer = new();
14− [SerializeField] private MusicDucker music = new();
15− [SerializeField] private Haptics haptics = new();
16− [SerializeField] private AudioClip explosionClip;
11+ [SerializeField] private AudioFacade audioFacade;
1712 [SerializeField] private float fuseSeconds = 2.5f;
1813
1914 private float timer;
20− private bool exploded;
2115
22− private void Awake() => pool.Init(transform);
16+ private void OnEnable() => timer = fuseSeconds;
2317
24− private void OnEnable()
25− {
26− timer = fuseSeconds;
27− exploded = false;
28− }
29−
3018 private void Update()
3119 {
32− if (exploded) return;
33−
3420 timer -= Time.deltaTime;
3521 if (timer > 0f) return;
3622
37− // The rocket, the barrel and the mine each repeat this sequence.
38− AudioSource source = pool.Rent(transform.position);
39− mixer.RouteToSfx(source);
40− source.PlayOneShot(explosionClip);
41− music.Duck();
42− haptics.Rumble(low: 0.8f, high: 0.4f);
43−
44− exploded = true; // stay active until the coroutine finishes
45− StartCoroutine(Cleanup(source));
46− }
47−
48− private IEnumerator Cleanup(AudioSource source)
49− {
50− yield return new WaitForSeconds(0.35f);
51− haptics.Stop();
52−
53− yield return new WaitForSeconds(Mathf.Max(0f, explosionClip.length - 0.35f));
54− music.Restore();
55− pool.Return(source);
23+ audioFacade.PlayExplosion(transform.position);
5624 gameObject.SetActive(false);
5725 }
5826 }
5927 }

Advantages & Disadvantages

+ Advantages

  • Clients become simple and decoupled from the details of each subsystem.
  • Shared recipes are defined once, so tuning an explosion is a one-file change.
  • Subsystems can be refactored or swapped behind the facade without touching callers.

− Disadvantages

  • Can turn into a god class if every new feature gets bolted on.
  • Callers that need fine control still have to go around it, so you maintain two paths.
  • Often implemented as a global singleton, which brings its own testing and coupling issues.

Tips

  1. 01Name facade methods after game intents (PlayExplosion, EnterCombat), not after the subsystems they touch.
  2. 02Inject the subsystems into the facade via [SerializeField] references so they remain visible and swappable in the Inspector.
  3. 03Keep the facade thin: it should orchestrate, not reimplement. Real logic belongs in the subsystems.
  4. 04Pair it with a Service Locator or DI container rather than a static Instance if you want testable callers.
  5. 05Use AudioMixer.FindSnapshot and TransitionTo inside the facade for ducking instead of manually lerping volumes.