0% found this document useful (0 votes)
5 views3 pages

Command - Observer Patterns

Uploaded by

emirbeldi13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Command - Observer Patterns

Uploaded by

emirbeldi13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

using System;

using System.Collections.Generic;

// Step 1: Define the ICommand interface.


interface ICommand
{
void Execute();
}

// Step 2: Create the Player class (Observer).


class Player : IObserver<ICommand>
{
private readonly string _playerName;

public Player(string playerName)


{
_playerName = playerName;
}

public void OnCompleted() { }

public void OnError(Exception error) { }

public void OnNext(ICommand command)


{
Console.WriteLine($"{_playerName} received a command.");
command.Execute();
}
}

// Step 3: Create the InputHandler class (Subject).


class InputHandler : IObservable<ICommand>
{
private List<IObserver<ICommand>> _observers = new List<IObserver<ICommand>>();

public IDisposable Subscribe(IObserver<ICommand> observer)


{
if (!_observers.Contains(observer))
_observers.Add(observer);

return new Unsubscriber(_observers, observer);


}

public void HandleInput(ICommand command)


{
foreach (var observer in _observers)
{
observer.OnNext(command);
}
}

private class Unsubscriber : IDisposable


{
private List<IObserver<ICommand>> _observers;
private IObserver<ICommand> _observer;

public Unsubscriber(List<IObserver<ICommand>> observers,


IObserver<ICommand> observer)
{
_observers = observers;
_observer = observer;
}

public void Dispose()


{
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");

inputHandler.HandleInput(jumpCommand);
inputHandler.HandleInput(attackCommand);
inputHandler.HandleInput(defendCommand);

// Unsubscribe player from InputHandler


inputHandler.Subscribe(player).Dispose();
}
}

You might also like