Advertisement
Guest User

CanSharePosition With - double dispatch

a guest
Nov 19th, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. Piece class
  2. -----------
  3.  
  4.     /**
  5.      * Check whether this piece can share its position
  6.      * with another piece.
  7.      *
  8.      * @param piece
  9.      *          The other piece to check.
  10.      *
  11.      * @return  False if the other piece is not effective.
  12.      *          | if (piece == null)
  13.      *          |   result == false
  14.      */
  15.     public abstract boolean canSharePositionWith(Piece piece);
  16.  
  17.  
  18.  
  19. Robot class
  20. --------------
  21. /**
  22.      * @return  False if the other piece is not effective.
  23.      *          | if (piece == null)
  24.      *          |   result == false
  25.      * @return  If the other piece is a robot, true if and only if
  26.      *          the other piece equals this robot.
  27.      *          | else if (piece instanceof Robot)
  28.      *          |   result == (piece == this)
  29.      * @return  True if the other piece is an item.
  30.      *          | else if (piece instanceof Item)
  31.      *          |   result == true
  32.      * @return  Otherwise, the call is dispatched to the other piece.
  33.      *          | else
  34.      *          |   result == piece.canSharePositionWith(this)
  35.      */
  36.     @Override
  37.     public boolean canSharePositionWith(Piece piece) {
  38.         if (piece == null)
  39.             return false;
  40.  
  41.         // Different robots cannot share positions
  42.         if (piece instanceof Robot)
  43.             return (piece == this);
  44.  
  45.         // Robots can share their position with items
  46.         if (piece instanceof Item)
  47.             return true;
  48.  
  49.         return piece.canSharePositionWith(this);
  50.     }
  51.  
  52.  
  53. Wall class
  54. ----
  55.  
  56.     /**
  57.      * @return  True if and only if the other piece equals this wall.
  58.      *          | result == (piece == this)
  59.      */
  60.     @Override
  61.     public boolean canSharePositionWith(Piece piece) {
  62.         // Walls cannot share their positions with other pieces
  63.         return (piece == this);
  64.     }
  65.  
  66.  
  67. Item class
  68. ---------/**
  69.      * @return  False if the other piece is not effective.
  70.      *          | if (piece == null)
  71.      *          |   result == false
  72.      * @return  True if the other piece is an item.
  73.      *          | else if (piece instanceof Item)
  74.      *          |   result == true
  75.      * @return  Otherwise, the call is dispatched to the other piece.
  76.      *          | else
  77.      *          |   result == piece.canSharePositionWith(this)
  78.      */
  79.     @Override
  80.     public boolean canSharePositionWith(Piece piece) {
  81.         if (piece == null)
  82.             return false;
  83.  
  84.         // Items can share their position with other items
  85.         if (piece instanceof Item)
  86.             return true;
  87.  
  88.         return piece.canSharePositionWith(this);
  89.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement