📘 Scenario:
The Justice League Command Center is equipped with a high-tech Control Panel (Invoker). Each button on this panel is wired to a Command object that knows what action to take and which superhero (Receiver) to call upon. When danger strikes, the panel simply triggers the right command—heroes leap into action without the panel knowing how it all works.
⚠️ Problem:
💡 Command Pattern Solution:
Each action is wrapped in a Command
object that decouples the invoker (panel) from the receiver (hero).
The Control Panel doesn't need to know how Superman flies or how Batman throws his batarang.
All it does is call execute()
on the command—and the magic happens behind the scenes.
🖥️ Output:
=== Crisis: Alien Drone Attack ===
[ControlPanel] Activating command...
[Superman] Flying at hypersonic speed to Metropolis!
[Superman] Rescuing civilians from collapsing buildings!
=== Crisis: Joker’s Drone Swarm ===
[ControlPanel] Activating command...
[Batman] Launching Batarang to disable the drone swarm!
=== Crisis: Collapsed Subway Tunnel ===
[ControlPanel] Activating command...
[Flash] Speed-blur rescuing victims in seconds!
Superman
Batman
Flash