๐ญ Facade With ๐งโ๐ป Singleton Managers Example
Facade in action: Escape rooms have lots of moving parts โ music, sound effects, items, characters. Managing each system directly clutters your code.
Why it matters: By making each manager a Singleton and wrapping them in a Facade, the game gets one simple interface. That means organized systems, cleaner code, and easy extensions down the road.
Concept
Section titled โConceptโ-
Each Manager (music, sounds, characters, items) is a Singleton โ only one exists.
-
A Facade provides a simple interface to interact with all of them.
-
The game doesnโt need to know about each manager โ just calls the Facade.
MusicManager (Singleton)
Section titled โMusicManager (Singleton)โpublic class MusicManager { private static MusicManager instance; private MusicManager() {}
public static MusicManager getInstance() { if (instance == null) { instance = new MusicManager(); } return instance; }
public void playBackgroundTrack(String track) { System.out.println("๐ต Playing background music: " + track); }
public void stopMusic() { System.out.println("๐ Music stopped."); }}SoundEffectManager (Singleton)
Section titled โSoundEffectManager (Singleton)โpublic class SoundEffectManager { private static SoundEffectManager instance; private SoundEffectManager() {}
public static SoundEffectManager getInstance() { if (instance == null) { instance = new SoundEffectManager(); } return instance; }
public void playEffect(String effect) { System.out.println("๐ Playing sound effect: " + effect); }}CharacterManager (Singleton)
Section titled โCharacterManager (Singleton)โimport java.util.ArrayList;import java.util.List;
public class CharacterManager { private static CharacterManager instance; private List<String> characters = new ArrayList<>();
private CharacterManager() {}
public static CharacterManager getInstance() { if (instance == null) { instance = new CharacterManager(); } return instance; }
public void addCharacter(String character) { characters.add(character); System.out.println("๐ค Added character: " + character); }
public void listCharacters() { System.out.println("๐ฅ Current characters: " + characters); }}ItemManager (Singleton)
Section titled โItemManager (Singleton)โimport java.util.ArrayList;import java.util.List;
public class ItemManager { private static ItemManager instance; private List<String> items = new ArrayList<>();
private ItemManager() {}
public static ItemManager getInstance() { if (instance == null) { instance = new ItemManager(); } return instance; }
public void addItem(String item) { items.add(item); System.out.println("๐ฆ Added item: " + item); }
public void listItems() { System.out.println("๐ Current items: " + items); }}Facade (GameFacade)
Section titled โFacade (GameFacade)โThis facade makes it easy to use all managers without needing to touch them directly.
public class GameFacade { private MusicManager musicManager = MusicManager.getInstance(); private SoundEffectManager soundManager = SoundEffectManager.getInstance(); private CharacterManager characterManager = CharacterManager.getInstance(); private ItemManager itemManager = ItemManager.getInstance();
public void startGame() { musicManager.playBackgroundTrack("escape_theme.mp3"); characterManager.addCharacter("Player"); itemManager.addItem("Flashlight"); System.out.println("๐ช Escape Room started!"); }
public void solvePuzzle() { soundManager.playEffect("puzzle_solved.wav"); itemManager.addItem("Key"); System.out.println("โ
Puzzle solved! A key has been added to your inventory."); }
public void endGame() { musicManager.stopMusic(); soundManager.playEffect("victory_fanfare.wav"); System.out.println("๐ Game completed!"); characterManager.listCharacters(); itemManager.listItems(); }}Using the Facade in the Game
Section titled โUsing the Facade in the Gameโpublic class EscapeRoomGame { public static void main(String[] args) { GameFacade game = new GameFacade();
game.startGame(); // Starts background music, adds player and flashlight game.solvePuzzle(); // Plays puzzle sound, adds key game.endGame(); // Stops music, plays victory sound, shows summary }}Sample Output
Section titled โSample Outputโ๐ต Playing background music: escape_theme.mp3๐ค Added character: Player๐ฆ Added item: Flashlight๐ช Escape Room started!๐ Playing sound effect: puzzle_solved.wav๐ฆ Added item: Keyโ
Puzzle solved! A key has been added to your inventory.๐ Music stopped.๐ Playing sound effect: victory_fanfare.wav๐ Game completed!๐ฅ Current characters: [Player]๐ Current items: [Flashlight, Key]Why This is Useful
Section titled โWhy This is Usefulโ-
Singleton Managers ensure consistency (only one music system, one inventory, etc.).
-
Facade provides a clean, simple API for the game โ no need to call each manager separately.
-
Extensible: If you add a
LightingManagerlater, you just extend the Facade, not the game logic.
Next Step Ideas ๐ก
Section titled โNext Step Ideas ๐กโ-
Add a Pause/Resume system in the facade (pause music, freeze characters).
-
Combine with Observer Pattern so managers notify UI when things change.
-
Use Factory Pattern to create items and characters inside the managers.
โ
Key Takeaway:
By combining Singleton Managers with a Facade, you get organized global systems and a simple interface for controlling them โ perfect for your escape roomโs audio, characters, and items.