Commit 79e0e51

mo_khan <mo@mokhan.ca>
2009-05-24 15:41:35
introduced registry for registering navigations and heading factories
1 parent 4c9ba6b
product/project/presentation/infrastructure/CallbackCommand.cs → product/project/common/CallbackCommand.cs
@@ -1,4 +1,4 @@
-namespace mars.rover.presentation.infrastructure
+namespace mars.rover.common
 {
     public interface CallbackCommand<T> : ParameterizedCommand<T>
     {
product/project/common/DefaultRegistry.cs
@@ -0,0 +1,35 @@
+using System.Collections;
+using System.Collections.Generic;
+
+namespace mars.rover.common
+{
+    public class DefaultRegistry<T> : Registry<T>
+    {
+        readonly IList<T> items;
+
+        public DefaultRegistry(params T[] items)
+        {
+            this.items = new List<T>(items);
+        }
+
+        public void Add(T item)
+        {
+            items.Add(item);
+        }
+
+        public IEnumerable<T> all()
+        {
+            return items;
+        }
+
+        public IEnumerator<T> GetEnumerator()
+        {
+            return all().GetEnumerator();
+        }
+
+        IEnumerator IEnumerable.GetEnumerator()
+        {
+            return GetEnumerator();
+        }
+    }
+}
\ No newline at end of file
product/project/presentation/infrastructure/ParameterizedCommand.cs → product/project/common/ParameterizedCommand.cs
@@ -1,4 +1,4 @@
-namespace mars.rover.presentation.infrastructure
+namespace mars.rover.common
 {
     public interface ParameterizedCommand<T>
     {
product/project/common/Registry.cs
@@ -0,0 +1,10 @@
+using System.Collections.Generic;
+
+namespace mars.rover.common
+{
+    public interface Registry<T> : IEnumerable<T>
+    {
+        IEnumerable<T> all();
+        void Add(T item);
+    }
+}
\ No newline at end of file
product/project/common/Specification.cs
@@ -0,0 +1,7 @@
+namespace mars.rover.common
+{
+    public interface Specification<T>
+    {
+        bool is_satisfied_by(T item);
+    }
+}
\ No newline at end of file
product/project/presentation/CaptureUserInstructionsPresenter.cs
@@ -1,6 +1,6 @@
 using System;
-using System.Collections.Generic;
 using System.Linq;
+using mars.rover.common;
 using mars.rover.domain;
 
 namespace mars.rover.presentation
@@ -8,28 +8,17 @@ namespace mars.rover.presentation
     public class CaptureUserInstructionsPresenter : Presenter
     {
         readonly CaptureUserInstructionsView view;
-        IList<HeadingFactory> factories;
-        IList<Navigation> navigations;
-        NASA nasa;
+        readonly Registry<HeadingFactory> factories;
+        readonly Registry<Navigation> navigations;
         Mars plateau;
         Rover rover;
 
-        public CaptureUserInstructionsPresenter(CaptureUserInstructionsView view)
+        public CaptureUserInstructionsPresenter(CaptureUserInstructionsView view, Registry<HeadingFactory> factories,
+                                                Registry<Navigation> navigations)
         {
             this.view = view;
-            factories = new List<HeadingFactory>
-                            {
-                                new DefaultHeadingFactory("N", x => new North(x)),
-                                new DefaultHeadingFactory("E", x => new East(x)),
-                                new DefaultHeadingFactory("W", x => new West(x)),
-                                new DefaultHeadingFactory("S", x => new South(x)),
-                            };
-            navigations = new List<Navigation>
-                              {
-                                  new Navigation('L', x => x.turn_left()),
-                                  new Navigation('R', x => x.turn_right()),
-                                  new Navigation('M', x => x.move_forward()),
-                              };
+            this.factories = factories;
+            this.navigations = navigations;
         }
 
         public virtual void run()
@@ -41,14 +30,13 @@ namespace mars.rover.presentation
         {
             var coordinates = line.Split(new[] {' '});
             plateau = new Mars(Convert.ToUInt32(coordinates[0]), Convert.ToUInt32(coordinates[1]));
-            nasa = new NASA(plateau);
         }
 
         public virtual void deploy_rover_to(string line)
         {
             var coordinates = line.Split(new[] {' '});
-            rover = nasa.deploy_rover_to(Convert.ToUInt32(coordinates[0]), Convert.ToUInt32(coordinates[1]),
-                                         find_heading_for(coordinates[2]));
+            rover = new Rover(Convert.ToUInt32(coordinates[0]), Convert.ToUInt32(coordinates[1]),
+                              find_heading_for(coordinates[2]));
         }
 
         public virtual void navigate_rover_using(string line)
@@ -64,58 +52,4 @@ namespace mars.rover.presentation
             return factories.Single(x => x.is_satisfied_by(heading)).create(plateau);
         }
     }
-
-    public interface HeadingFactory : Specification<string>
-    {
-        Heading create(Plateau plateau);
-    }
-
-    public class Navigation : Specification<char>
-    {
-        readonly char command_text;
-        readonly Action<Rover> navigation;
-
-        public Navigation(char command_text, Action<Rover> navigation)
-        {
-            this.command_text = command_text;
-            this.navigation = navigation;
-        }
-
-        public virtual void navigate(Rover rover)
-        {
-            navigation(rover);
-        }
-
-        public virtual bool is_satisfied_by(char item)
-        {
-            return char.ToUpperInvariant(command_text).Equals(char.ToUpperInvariant(item));
-        }
-    }
-
-    public class DefaultHeadingFactory : HeadingFactory
-    {
-        string code;
-        Func<Plateau, Heading> factory;
-
-        public DefaultHeadingFactory(string code, Func<Plateau, Heading> factory)
-        {
-            this.code = code;
-            this.factory = factory;
-        }
-
-        public bool is_satisfied_by(string item)
-        {
-            return string.Equals(code, item, StringComparison.OrdinalIgnoreCase);
-        }
-
-        public Heading create(Plateau plateau)
-        {
-            return factory(plateau);
-        }
-    }
-
-    public interface Specification<T>
-    {
-        bool is_satisfied_by(T item);
-    }
 }
\ No newline at end of file
product/project/presentation/CommandLineArgument.cs
@@ -13,5 +13,30 @@ namespace mars.rover.presentation
         {
             return new CommandLineArgument(argument);
         }
+
+        public override string ToString()
+        {
+            return argument;
+        }
+
+        public bool Equals(CommandLineArgument other)
+        {
+            if (ReferenceEquals(null, other)) return false;
+            if (ReferenceEquals(this, other)) return true;
+            return Equals(other.argument, argument);
+        }
+
+        public override bool Equals(object obj)
+        {
+            if (ReferenceEquals(null, obj)) return false;
+            if (ReferenceEquals(this, obj)) return true;
+            if (obj.GetType() != typeof (CommandLineArgument)) return false;
+            return Equals((CommandLineArgument) obj);
+        }
+
+        public override int GetHashCode()
+        {
+            return (argument != null ? argument.GetHashCode() : 0);
+        }
     }
 }
\ No newline at end of file
product/project/presentation/DefaultHeadingFactory.cs
@@ -0,0 +1,27 @@
+using System;
+using mars.rover.domain;
+
+namespace mars.rover.presentation
+{
+    public class DefaultHeadingFactory : HeadingFactory
+    {
+        readonly string code;
+        readonly Func<Plateau, Heading> factory;
+
+        public DefaultHeadingFactory(string code, Func<Plateau, Heading> factory)
+        {
+            this.code = code;
+            this.factory = factory;
+        }
+
+        public virtual bool is_satisfied_by(string item)
+        {
+            return string.Equals(code, item, StringComparison.OrdinalIgnoreCase);
+        }
+
+        public virtual Heading create(Plateau plateau)
+        {
+            return factory(plateau);
+        }
+    }
+}
\ No newline at end of file
product/project/presentation/HeadingFactory.cs
@@ -0,0 +1,10 @@
+using mars.rover.common;
+using mars.rover.domain;
+
+namespace mars.rover.presentation
+{
+    public interface HeadingFactory : Specification<string>
+    {
+        Heading create(Plateau plateau);
+    }
+}
\ No newline at end of file
product/project/presentation/Navigation.cs
@@ -0,0 +1,28 @@
+using System;
+using mars.rover.common;
+using mars.rover.domain;
+
+namespace mars.rover.presentation
+{
+    public class Navigation : Specification<char>
+    {
+        readonly char command_text;
+        readonly Action<Rover> navigation;
+
+        public Navigation(char command_text, Action<Rover> navigation)
+        {
+            this.command_text = command_text;
+            this.navigation = navigation;
+        }
+
+        public virtual void navigate(Rover rover)
+        {
+            navigation(rover);
+        }
+
+        public virtual bool is_satisfied_by(char item)
+        {
+            return char.ToUpperInvariant(command_text).Equals(char.ToUpperInvariant(item));
+        }
+    }
+}
\ No newline at end of file
product/project/Program.cs
@@ -1,8 +1,9 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using mars.rover.common;
+using mars.rover.domain;
 using mars.rover.presentation;
-using mars.rover.presentation.infrastructure;
 
 namespace mars.rover
 {
@@ -22,8 +23,24 @@ namespace mars.rover
 
         static void Main(string[] args)
         {
-            new Program(new CaptureUserInstructionsPresenter(new CaptureUserInstructionsConsoleView(Console.In,Console.Out)))
-                .run_with( args.Select(x => (CommandLineArgument) x));
+            var program = new Program(
+                new CaptureUserInstructionsPresenter(
+                    new CaptureUserInstructionsConsoleView(Console.In, Console.Out),
+                    new DefaultRegistry<HeadingFactory>
+                        {
+                            new DefaultHeadingFactory("N", x => new North(x)),
+                            new DefaultHeadingFactory("E", x => new East(x)),
+                            new DefaultHeadingFactory("W", x => new West(x)),
+                            new DefaultHeadingFactory("S", x => new South(x)),
+                        },
+                    new DefaultRegistry<Navigation>
+                        {
+                            new Navigation('L', x => x.turn_left()),
+                            new Navigation('R', x => x.turn_right()),
+                            new Navigation('M', x => x.move_forward()),
+                        }
+                    ));
+            program.run_with(args.Select(x => (CommandLineArgument) x));
         }
     }
 }
\ No newline at end of file
product/project/project.csproj
@@ -45,11 +45,15 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="presentation\infrastructure\CallbackCommand.cs" />
+    <Compile Include="common\DefaultRegistry.cs" />
+    <Compile Include="common\Registry.cs" />
+    <Compile Include="presentation\DefaultHeadingFactory.cs" />
+    <Compile Include="common\CallbackCommand.cs" />
     <Compile Include="presentation\CaptureUserInstructionsConsoleView.cs" />
     <Compile Include="presentation\CaptureUserInstructionsView.cs" />
     <Compile Include="common\Command.cs" />
     <Compile Include="presentation\CommandLineArgument.cs" />
+    <Compile Include="presentation\HeadingFactory.cs" />
     <Compile Include="presentation\infrastructure\CommandProcessor.cs" />
     <Compile Include="domain\Coordinate.cs" />
     <Compile Include="domain\East.cs" />
@@ -57,11 +61,13 @@
     <Compile Include="domain\Headings.cs" />
     <Compile Include="domain\NASA.cs" />
     <Compile Include="domain\North.cs" />
-    <Compile Include="presentation\infrastructure\ParameterizedCommand.cs" />
+    <Compile Include="common\ParameterizedCommand.cs" />
     <Compile Include="domain\Mars.cs" />
     <Compile Include="domain\Plateau.cs" />
+    <Compile Include="presentation\Navigation.cs" />
     <Compile Include="presentation\Presenter.cs" />
     <Compile Include="presentation\CaptureUserInstructionsPresenter.cs" />
+    <Compile Include="common\Specification.cs" />
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="domain\Rover.cs" />
product/project.specifications/ProgramSpecs.cs
@@ -3,8 +3,8 @@ using developwithpassion.bdd;
 using developwithpassion.bdd.contexts;
 using developwithpassion.bdd.mbunit.standard.observations;
 using mars.rover;
+using mars.rover.common;
 using mars.rover.presentation;
-using mars.rover.presentation.infrastructure;
 
 namespace specifications
 {