寻找更好的设计......哪种设计模式可以用于这些算法? [英] Looking for a better design...which design pattern can use for these algorithems?

查看:79
本文介绍了寻找更好的设计......哪种设计模式可以用于这些算法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经解决了这个问题,例如,下面我解释的问题是:b $ b

问题。我正在寻找algorithem的设计模式或者是否有任何设计模式更好的解决方因为根据下面的要求,它说algorithem应该可以互换,不应该实现IHardware接口。命令模式适合清理算法?



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?

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是一家生产清洁机器人的公司。我们创建了这个机器人的软件,我们购买硬件。我们与硬件提供商签订了合同,这是一个接口,还有一个用于测试的硬件假类。



我们的机器人总是在没有障碍物的矩形房间里工作。始终从左上角开始(坐标x = 0,y = 0),指向东方(FaceTo = 0)。我们的机器人有一个传感器来检测它前面是否有墙(IsObstacle()= true)。





你有这个伪代码算法来打扫房间:



1)向左旋转。



2)如果有这是一个障碍,机器人从不在那里,走路。



3)如果没有,请向右旋转。



4)如果没有障碍物且机器人从不在那里,请走路。



5)如果没有,请向右旋转。



6)如果没有障碍物并且机器人从不在那里,那就走吧。



7)如果没有,房间打扫干净。





打扫房间后,您需要尽快返回左上角。不要在意再次使用清洁过的细胞。





最后,你需要打印用来清洁细胞的路径房间(所有使用的单元格,按照已清理的顺序)和机器人使用的移动总数(打印属性TotalMovements)







要求



1)实施清洁算法。



2)实现返回算法。



3)两种算法都需要易于互换。



4)软件不得与硬件配合使用,此硬件需要易于互换。



5)打扫房间后,我们需要知道使用的路径和运动总数。





澄清



1)您必须使用提供的硬件类。你不能实现接口或你自己的硬件类。



2)由于硬件类是假类,它需要房间的大小(即4x5) )在构造函数中。你需要将这些信息传递给构造函数,但你不能在你的班级中使用这些信息。



3)只有在你习惯使用它时才使用TDD 。专注于设计和实施。



4)干净的代码和良好的命名是积极的。



5)机器人不会提醒路径。



6)不关心GUI。你可以使用控制台应用程序,测试类或任何你需要的东西。





接口IHardwareRobot








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


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

    }

推荐答案

嗨Murali,



请找我的解决方案如下。



1.两种算法都需要易于互换。

         [Beryl]使用策略模式启用此功能。请对返回算法执行相同操作

2.软件不得与硬件配合使用,并且此硬件需要易于互换

          [Beryl]适配器模式处理这一点。

3.用于TDD目的的IRobot



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

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
    {
    }



问候,

Beryl Wilson


Regards,
Beryl Wilson


这篇关于寻找更好的设计......哪种设计模式可以用于这些算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆