dimanche 10 février 2019

Class design for keeping track of visited nodes

I am stuck at trying to decide something in my class design. This is the overview of what I need to design:

  1. Robot that paints blocks on a square grid (a grid is collection of n blocks).
  2. Let's say there are 4 types of blocks in a grid and each block could cost different amount to paint.
  3. User will input command to instruct robot which block to paint.
  4. If block was already painted, there will be penalty to repaint it.
  5. Finally, report total cost (split by block types), and commands issued by the user.

Some of the things that I have modeled are as follows:

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

For issuing commands, I modeled them as Command design pattern.

Question:

I am getting confused on is in which class should the visited (aka painted) blocks be stored (to handle #4 and #5 above)?

  • Should I store them in Robot? (I didn't thing it belonged in Robot because it felt like tight coupling between the notion of Robot and paintable block.)

  • I didn't want to store it in Grid as well because, again, I don't think that Grid needs to know what action is being taken on it (not too sure about this though).

  • I could've stored in different class (say Foo) but then I thought that may be user can issue a command like where ever Robot is, paint next 2 blocks. In this case, since this would be executed in PaintCommand (handled by CommandPattern), Foo wouldn't know what blocks have been painted.

Please let me know your thoughts.

Aucun commentaire:

Enregistrer un commentaire