Pattern Labs

Online Auctions

An online auction consists of an auctioneer, an item to be sold, a timer, and bidders. Each time a bidder makes an offer that exceeds the current best offer, the other bidders are notified. The auctioneer keeps track of the highest bidder. When the timer reaches 0, the auctioneer is notified. The auction ends and the item is sold to the highest bidder. Note that the item has an initial price. No bids below the initial price are accepted. Of course bidders use different strategies to determine what to offer. For example:

Bid $10 over the last offer
Bid $10 over the last offer but not to exceed $100
Bid $10 over the last offer after 10 bids have been made
Bid $N over last offer where N = 10, 9, 8, ...

Be careful, a bidder should never bid against himself.

Draw a UML class diagram showing how you would design this online auction. Use the Strategy Pattern and the Publisher-Subscriber Pattern.

Digital Circuit Simulator

A wire encapsulates a single Boolean value.

A digital component has an array of n input wires, and an array of m output wires (n and m are set by the constructor), and an abstract eval method that sets the values of the output wires according to the values of the input wires.

There are two types of digital components: gates and circuits.

There are three types of gates: AND, OR, and NOT gates. Each has a single output wire. A NOT gate has a single input wire while OR and AND gates can have 2 or more input wires.

A circuit contains many internal wires and components. For example, a circuit might contain two internal components, A and B and a wire W, where W is both an output wire of A and an input wire of B. Of course W might be the output wire of several internal components.

Here is the circuit diagram for a 1-bit half-adder, which is a type of digital circuit:

hadd

Note that s0 = a0 + b0 and cout is the carry out.

Draw a UML class diagram showing how you would design a toolkit for modeling digital components. Use the Composite Design Pattern and the Publisher-Subscriber Pattern. Wires are publishers and components are subscribers. A component subscribes to all of its input wires. When a wire changes value, it notifies all of its subscriber components, which call their eval methods.

Implement and test the following classes according to your design:

Wire
AndGate

Maze Challenge

A player moves from room to room through a seemingly endless maze, desperately seeking the exit before he runs out of time.

The maze is an N x M array of rooms:

class Maze ... {
   int MAX_ROW, MAX_COL;
   Room[][] maze;

Two special rooms are marked:

   int playerRow, playerCol; // current position of the player
   int exitRow, exitCol;     // position of exit

A counter counts the number of player moves:

   int playerMoves = 0;

Of course there are setters and getters associated with each of these fields (except no setters for MAX_ROW and MAX_COL).

For now a room is an empty object:

class Room { }

Here's a sketch of a maze controller:

class MazeController {
   private Maze myMaze;
   void execute(String command) throws Exception {

   }
}

In particular, a maze controller executes the following commands for moving the player around the maze:

NORTH, EAST, WEST, SOUTH, RESET

For example:

else if (command.equals("SOUTH")) {
   int col = myMaze.getPlayerCol();
   int moves = myMaze.getPlayerMoves();
   col = (col + 1) % myMaze.getMAX_COL();
   myMaze.setPlayerCol(col);
   myMaze.setPlayerMoves(moves + 1);
}

(i) Implement and test Maze and MazeController.

Here is the GUI for Maze Challenge (ignore the File menu for now):

(ii) Draw a UML class diagram showing how you would implement Maze Challenge. Your design should incorporate the following patterns:

Model-View-Controller
Publisher-Subscriber
Adapter

Use the adapter pattern to create a listener that adapts a maze controller to the ActionListener interface.

Use the Publisher-Subscriber pattern to update the GUI each time one of the maze fields changes.

(iii) Implement and test Maze Challenge.

Your GUI should pop up dialog boxes when the number of moves left reaches 0 or when the player reaches the exit. For example:

Hint: use JOptionPane for this.