{ if (_observers.Contains(_observer)) _observers.Remove(_observer); } } }
// Step 4: Implement the Concrete Command classes.
class JumpCommand : ICommand { private readonly string _playerName;
public JumpCommand(string playerName)
{ _playerName = playerName; }
public void Execute()
{ Console.WriteLine($"{_playerName} is jumping."); } }
class AttackCommand : ICommand
{ private readonly string _playerName;
public AttackCommand(string playerName)
{ _playerName = playerName; }
public void Execute()
{ Console.WriteLine($"{_playerName} is attacking."); } }
class DefendCommand : ICommand
{ private readonly string _playerName;
public DefendCommand(string playerName)
{ _playerName = playerName; }
public void Execute()
{ Console.WriteLine($"{_playerName} is defending."); } }
// Client code (Main method)
class Program { static void Main() { InputHandler inputHandler = new InputHandler();
// Create player and subscribe to InputHandler
Player player = new Player("Player1"); inputHandler.Subscribe(player);
// Simulate player input
ICommand jumpCommand = new JumpCommand("Player1"); ICommand attackCommand = new AttackCommand("Player1"); ICommand defendCommand = new DefendCommand("Player1");