Applying design patterns
Upcoming SlideShare
Loading in...5
×
 

Applying design patterns

on

  • 216 views

We solve Sudoku by following certain routines in our mind. Thus we have to apply handful of patterns to solve each of these routines. I am taking just one of these patterns good enough for solving ...

We solve Sudoku by following certain routines in our mind. Thus we have to apply handful of patterns to solve each of these routines. I am taking just one of these patterns good enough for solving easy ones and delve in to detail.

Statistics

Views

Total Views
216
Views on SlideShare
216
Embed Views
0

Actions

Likes
0
Downloads
2
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Applying design patterns Applying design patterns Presentation Transcript

  • Applying Design Patterns Solving Sudoku, easy ones
  • Problem
  • Design Patterns Problem Pattern Solution
  • Applying Patterns • Break the problem down to common known problems • Apply Known Solutions
  • A Common Problem • When a number is placed in a cell, the adjacent cells loose the opportunity to have this number • How to make the adjacent cell react on placement of a number?
  • A Known Solution • Cells that are adjacent to a cell can be made to observe and react on placement of a number using Observer Pattern
  • Setup • To start with, each cell can have any value between 1 and 9, inclusive, with equal probability. public class Cell { private List values = new ArrayList(); private int row; private int col; // Add all possible entries (1 to 9) in each cell public Cell(int row, int col) { this.row = row; this.col = col; for (int n = 1; n <= 9; n++) { values.add(new Integer(n)); } } }
  • Cell as Observable and Observer public class Cell extends Observable implements Observer { //… public synchronized void addObserver(Cell[][] cells) { //… loop thru and add adjacent cells as observers super.addObserver(cells[i][j]); //… } //notify observers while setting the known value in the cell public void setValue(int value) { super.notifyObservers(new Integer(value)); } // Observe and remove the entry set in the observable public void update(Observable o, Object arg) { values.remove(arg); } } • Observable implementation in Java has provision for adding objects implementing Observer interface and notifying them by calling the update method.
  • Demo • Below is the code for creating a collection of cell and setting up the values. • It also demonstrates with a sample. • http://code.google.com/p/observerpattern- sudoku/source/browse/