アクション デリゲートのキューを使用して GOF コマンド パターンを実装することは可能ですか?
しばらくの間、頭を抱えようとしてきましたが、キューに追加したい可能なアクションのそれぞれにさまざまな数のパラメーターがある可能性があるため、困惑しています。
助言がありますか?コマンド パターンに注目して間違ったツリーを鳴らしていませんか?
アップデート:
jgauffinに感謝します。うまくいきます...私の実装は次のようになります
public class CommandDispatcher
{
private readonly Dictionary<Type, List<Action<ICommand>>> _registeredCommands =
new Dictionary<Type, List<Action<ICommand>>>();
public void RegisterCommand<T>(Action<ICommand> action) where T : ICommand
{
if (_registeredCommands.ContainsKey(typeof (T)))
_registeredCommands[typeof (T)].Add(action);
else
_registeredCommands.Add(typeof (T), new List<Action<ICommand>> {action});
}
public void Trigger<T>(T command) where T : ICommand
{
if (!_registeredCommands.ContainsKey(typeof(T)))
throw new InvalidOperationException("There are no subscribers for that command");
foreach (var registeredCommand in _registeredCommands[typeof(T)])
{
registeredCommand(command);
if (command.Cancel) break;
}
}
}