用于跟踪访问的节点的类设计

时间:2019-02-10 10:12:48

标签: oop design-patterns class-design

我坚持尝试在课堂设计中做出决定。这是我需要设计的概述:

  1. 在正方形网格上绘制块的机器人(一个网格是n个块的集合)。
  2. 假设网格中有4种类型的块,并且每个块可能花费不同的绘制量。
  3. 用户将输入命令来指示机器人要喷涂哪个块。
  4. 如果已经绘制了块,则将被罚款。
  5. 最后,报告总成本(按块类型划分)和用户发出的命令。

我建模的一些东西如下:

class Robot {
    // get current location
    // place it at given location 
}

class Grid { 
    // Grid is a collection of blocks
    Block[][] blocks; 
} 

class Block {
    // each block has it's coordinates, 
    // has paint status (painted or unpainted), 
    // and accepts a visitor to determine price to paint
} 

class SquareBlock extends Block {
} 

class RectangularBlock extends Block {
} 

对于发出命令,我将它们建模为Command design pattern

问题

让我感到困惑的是,应该将visited(又称涂色)块存储在哪个类中(以处理上面的#4和#5)?

  • 我应该将它们存储在Robot中吗? (我并不认为它属于Robot,因为它感觉Robotpaintable block的概念之间紧密耦合。)

  • 我也不想将其存储在Grid中,因为同样,我不认为Grid不需要知道对其采取了什么措施(不是太当然可以)。

  • 我可以将其存储在不同的类中(例如Foo),但是后来我认为可能是用户可以发出类似where ever Robot is, paint next 2 blocks的命令。在这种情况下,由于将在PaintCommand中执行(由CommandPattern处理),因此Foo将不知道绘制了哪些块。

请让我知道您的想法。

0 个答案:

没有答案