Commit 8c03ead

mo_khan <mo@mokhan.ca>
2009-05-25 02:21:24
refactoring command pump
1 parent 14e97ee
Changed files (3)
product/project/presentation/infrastructure/CommandPump.cs
@@ -2,8 +2,8 @@ using mars.rover.common;
 
 namespace mars.rover.presentation.infrastructure
 {
-    public interface CommandPump<Input> : Command
+    public interface CommandPump : Command
     {
-        void run<Command>(Input input) where Command : ParameterizedCommand<Input>;
+        void run<Command, Input>(Input input) where Command : ParameterizedCommand<Input>;
     }
 }
\ No newline at end of file
product/project/presentation/infrastructure/SynchronousCommandPump.cs
@@ -3,32 +3,48 @@ using mars.rover.common;
 
 namespace mars.rover.presentation.infrastructure
 {
-    public class SynchronousCommandPump : CommandPump<string>
+    public class SynchronousCommandPump : CommandPump
     {
         readonly Registry<ParameterizedCommand<string>> commands;
         readonly CommandProcessor processor;
         readonly CommandFactory factory;
 
-        public SynchronousCommandPump(Registry<ParameterizedCommand<string>> commands, CommandProcessor processor, CommandFactory factory)
+        public SynchronousCommandPump(Registry<ParameterizedCommand<string>> commands, CommandProcessor processor,
+                                      CommandFactory factory)
         {
             this.commands = commands;
             this.processor = processor;
             this.factory = factory;
         }
 
-        public virtual void run<Command>(string input) where Command : ParameterizedCommand<string>
+        public virtual void run<Command, Input>(Input input) where Command : ParameterizedCommand<Input>
         {
-            processor.add(factory.create_for(() => get<Command>().run_against(input)));
+            processor.add(factory.create_for(() => get<Command, Input>().run_against(input)));
         }
 
-        ParameterizedCommand<string> get<T>()
+        public virtual void run()
         {
-            return commands.First(y => y is T);
+            processor.run();
         }
 
-        public virtual void run()
+        ParameterizedCommand<Input> get<Command, Input>()
         {
-            processor.run();
+            return new AdaptedCommand<Input>(commands.First(y => y is Command));
+        }
+
+        class AdaptedCommand<T> : ParameterizedCommand<T>
+        {
+            readonly ParameterizedCommand<string> command;
+
+            public AdaptedCommand(ParameterizedCommand<string> command)
+            {
+                this.command = command;
+            }
+
+            public void run_against(T item)
+            {
+                command.run_against(item as string);
+            }
         }
     }
 }
\ No newline at end of file
product/project/presentation/CaptureUserInstructionsPresenter.cs
@@ -5,9 +5,9 @@ namespace mars.rover.presentation
     public class CaptureUserInstructionsPresenter : Presenter
     {
         readonly CaptureUserInstructionsView view;
-        readonly CommandPump<string> pump;
+        readonly CommandPump pump;
 
-        public CaptureUserInstructionsPresenter(CaptureUserInstructionsView view, CommandPump<string> pump)
+        public CaptureUserInstructionsPresenter(CaptureUserInstructionsView view, CommandPump pump)
         {
             this.view = view;
             this.pump = pump;
@@ -20,17 +20,17 @@ namespace mars.rover.presentation
 
         public virtual void provide_upper_right_coordinates(string line)
         {
-            pump.run<CreateMarsCommand>(line);
+            pump.run<CreateMarsCommand, string>(line);
         }
 
         public virtual void deploy_rover_to(string deployment_coordinates)
         {
-            pump.run<DeployRoverCommand>(deployment_coordinates);
+            pump.run<DeployRoverCommand, string>(deployment_coordinates);
         }
 
         public virtual void navigate_rover_using(string navigation_commands)
         {
-            pump.run<NavigateRoverCommand>(navigation_commands);
+            pump.run<NavigateRoverCommand, string>(navigation_commands);
         }
 
         public void go()