Click here to Skip to main content
15,867,950 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have deigned the problem like,

problem explained below.I am looking for design pattern for both algorithem or is there any better solution? Since as per the below requirement it says algorithem should be interchangeable and should not implement the IHardware interface.Command patter is suitable for cleaning algo?

C#
public interface ICleaningAlgorithm {
    void Clean(IRobot robot);
}

public interface IReturnAlgorithm {
   void Return(IRobot robot);
}
Example classes (without implementation):

public class CleaningAlgorithm : ICleaningAlgorithm {
    public void Clean(IRobot robot) {
        /* pseudocode from the post */
    }
}

public class ReturnAlgorithm {
   public void Return(IRobot robot) {
       /* some shortest path algorithm */
   }
}



Rumba Mumba Robots is a company that creates cleaning robots. We creates the software of this robots and we buy the hardware. We’ve a contract with the hardware provider that is an interface, and a Hardware Fake class for testing.

Our robots always work in rectangular rooms, without obstacles. Always start in the top left corner (coordinates x=0, y=0), pointing to the east (FaceTo = 0). Our robot has a sensor to detect if it has a wall in front of it (IsObstacle()=true).


You have this pseudocode algorithm to clean the room:

1) Rotate left.

2) If there isn’t an obstacle and the robot was never there, walk.

3) If not, rotate right.

4) If there isn’t an obstacle and the robot was never there, walk.

5) If not, rotate right.

6) If there isn’t an obstacle and the robot was never there, walk.

7) If not, the room is cleaned.


After clean the room, you need to return to the top left corner as fast as you can. Don’t care about use again cleaned cells.


At the end, you need to print the path that you use to clean the room (all the cells used, in the order that has been cleaned) and the total of movement the robot has used (printing the property TotalMovements)



REQUIREMENTS

1) Implement the cleaning algorithm.

2) Implement the return algorithm.

3) Both algorithm need to be easily interchangeable.

4) The software mustn’t be coupled with the hardware, and this hardware need to be easily interchangeable.

5) After clean the room, we need to know the path used and the total of movements.


CLARIFICATIONS

1) You must use the class Hardware provided. You mustn’t implement the interface or your own hardware class.

2) AS the Hardware class is a fake class, it needs the size of the room (i.e. 4x5) in the constructor. You need to pass this information to the constructor, but you mustn’t use this information in your class.

3) Use TDD only if you’re comfortable using it. Focus on the design and the implementation.

4) Clean code and good naming are positives.

5) The robot doesn’t remind the path.

6) Don’t care about the GUI. You can use a Console Application, Test classes or whatever you need.


Interface IHardwareRobot


C#
public interface IHardwareRobot

    {

        /// <summary>

        /// Return true if there's an obstacle in front of the robot.

        /// </summary>

        /// <returns></returns>

        bool IsObstacle();

        /// <summary>

        /// Turn 90 degrees to the right.

        /// </summary>

        void TurnRight();

        /// <summary>

        /// Turn 90 degrees to the left.

        /// </summary>

        void TurnLeft();

        /// <summary>

        /// Walk 1 meter to the front (1 cell).

        /// </summary>

        void Walk();

        /// <summary>

        /// Set movement to 0 & go back to the top left corner, facing to the East.

        /// </summary>

        void Reset();

        /// <summary>

        /// Total movements. TurnRight, TurnLeft and Walk are movements. Is the amount of battery that we spent.

        /// </summary>

        int TotalMovements { get; }

        /// <summary>

        /// X coordinate 0 West Increase going to the east

        /// </summary>

        int X { get; }

        /// <summary>

        /// Y coordinate 0 North Increase going to the south

        /// </summary>

        int Y { get; }

        /// <summary>

        /// 0=E,1=S,2=W,3=N

        /// </summary>

        int FaceTo { get; }//0=E,1=S,2=W,3=N

    }
Posted
Updated 13-Jul-13 3:39am
v3
Comments
BillWoodruff 9-Sep-13 0:20am    
"Always start in the top left corner (coordinates x=0, y=0), pointing to the east (FaceTo = 0). ... 1) Rotate left."

The correct answer to this homework problem (Master's degree level ?) is that no solution is possible given these constraints. Assuming "east" means a forward movement, left-to-right, along the x-axis, and the robot is at 0,0 "facing east", a left-turn is impossible because the left-side (now, it's top-side) edge of the robot is directly against the top wall.

1 solution

Hi Murali,

Please find my solution below.

1. Both algorithm need to be easily interchangeable.
        [Beryl] Used strategy pattern to enable this. Please do the same for return algorithm
2. The software mustn’t be coupled with the hardware, and this hardware need to be easily interchangeable
        [Beryl] Adaptor pattern takes care of this point.
3. IRobot used for TDD purpose

C#
public class Robot : IRobot
    {
        private CleaningOptions cleaningOptions = CleaningOptions.Easy;
        private CleaningContext cleaningContext;
        private VendorHardwareAdaptor hardwareAdaptor;

        public CleaningOptions CleaningOptions
        {
            get
            {
                return this.cleaningOptions;
            }
            set
            {
                this.cleaningOptions = value;
            }
        }
    }

public enum CleaningOptions
    {
        Easy,
        Normal,
    }

public class CleaningContext
    {
        private IDictionary<cleaningoptions,> cleaningStrategies;

        public CleaningContext()
        {
            this.cleaningStrategies = new Dictionary<cleaningoptions,>();

            this.cleaningStrategies.Add(CleaningOptions.Easy, new EasyCleaningStrategy());
            this.cleaningStrategies.Add(CleaningOptions.Normal, new NormalCleaningStrategy());
        }

        public void Clean(CleaningOptions option)
        {
            if(!this.cleaningStrategies.ContainsKey(option))
                throw new NotSupportedException();

            this.cleaningStrategies[option].Clean();
        }
    }

public class EasyCleaningStrategy : ICleaningStrategy
    {
        public void Clean()
        {
            throw new NotImplementedException();
        }
    }

public interface ICleaningStrategy
    {
        void Clean();
    }

public interface IRumbaHardware
    {
    }

public class VendorHardwareAdaptor : Hardware, IRumbaHardware
    {
    }

Regards,
Beryl Wilson
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900