Reputation: 6728
I have a Commander
class, which handles commands. All these commands implement the ICommand
interface. Basically the command pattern...
Now I want to create something similar to an event
for each specific type of command, without actually making an event for each specific type in the commander. The commander should not be coupled to each type of command.
So my command has a method void Subscribe<T>(Action<T> callback) where T: ICommand
. If a subscriber calls this with the method void MyAttackCommandHandler(AttackCommand att)
as the parameter, I expect the subscriber to get a callback only for AttackCommands
. However another class can also subscribe for a different command.
I tried creating a dictionary, that maps the type of the parameter (the kind of command) to a list of subscribers: Dictionary<Type, List<Action<ICommand>>> _subscriptions
, and then my subscribe method would look something like:
public void Subscribe<T>(Action<T> callback)
where T: ICommand
{
Type type = typeof(T);
if (_subscriptions.ContainsKey(type))
{
List<Action<ICommand>> subscribtions = _subscriptions[type];
subscribtions.Add(callback);
}
else ... //create a new entry in _subscriptions
}
This however doesn't work because callback
isn't of the type Action<ICommand>
, but of Action<AttackCommand>
for instance.
How would one implement this cleanly?
Thanks!
Upvotes: 1
Views: 473
Reputation: 7618
Try this
subscribtions.Add(i => callback((T)i));
If the above doesn't work please provide a full example that shows your problem. Something like this:
using System;
using System.Collections.Generic;
namespace Example
{
class Program
{
static void Main(string[] args)
{
Commander C = new Commander();
C.Subscribe((MyCommand i) => { Console.WriteLine(i.Value); });
C.Subscribe((SquareMyCommand i) => { Console.WriteLine(i.Value); });
C.Subscribe((SquareMyCommand i) => { Console.WriteLine("**" + i.Value + "**"); });
C.Do(new MyCommand(2));//1 callback , Prints 2
C.Do(new SquareMyCommand(3));//2 callbacks, Prints 9 , **9**
Console.ReadLine();
}
}
public class Commander
{
Dictionary<Type, List<Action<ICommand>>> dictionary = new Dictionary<Type, List<Action<ICommand>>>();
public void Subscribe<T>(Action<T> callback) where T : ICommand
{
Type type = typeof(T);
List<Action<ICommand>> subscribtions = null;
dictionary.TryGetValue(type, out subscribtions);
if (subscribtions == null)
{
subscribtions = new List<Action<ICommand>>();
dictionary.Add(type, subscribtions);
}
subscribtions.Add(i => callback((T)i));
}
public void Do<T>(T t) where T : ICommand
{
foreach (var item in dictionary[t.GetType()])
item(t);
}
}
public class MyCommand : ICommand
{
public MyCommand(int x) { Value = x; }
public int Value { get; set; }
}
public class SquareMyCommand : ICommand
{
public SquareMyCommand(int x) { Value = x * x; }
public int Value { get; set; }
}
public interface ICommand
{
int Value { get; set; }
}
}
Upvotes: 3