Applying design patterns

40 %
60 %
Information about Applying design patterns

Published on March 12, 2014

Author: chellash



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.

Applying Design Patterns Solving Sudoku, easy ones


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. • sudoku/source/browse/

Add a comment

Related presentations

Related pages

The First Step to Applying Design Patterns: Don't :: Mike ...

In the previous section on Applying Design Patterns, I said that all problems have been solved. This is, of course, not true.
Read more

Applying UML and Patterns: An Introduction to Object ...

Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development: Craig Larman: Fremdsprachige Bücher
Read more

Part I - Design Your Soccer Engine, and Learn How To Apply ...

Design Your Soccer Engine, and Learn How To Apply Design Patterns (Observer, Decorator, Strategy and Builder Patterns) - Part I and II
Read more

Software Design Patterns Made Simple - Scribd - Read ...

Software Design Patterns Made Simple - Free download as PDF File (.pdf), Text file (.txt) or read online for free. Introducing software design patterns to ...
Read more

Practically Applying Design Patterns - Part I and II

In this case, we used this pattern because we need to notify all the players, when the position of the ball is changed. 2: Addressing the design problems ...
Read more

Applying Domain-Driven Design and Patterns: Using .Net ...

Synopsis Applying Domain-Driven Design and Patterns is the first complete, practical guide to leveraging patterns, domain-driven design, and test-driven ...
Read more

How do you apply design patterns? - Stack Overflow

I think there is a tendency, at least for those who newly learn design patterns, to over-apply a pattern; when you have a hammer, everything starts to look ...
Read more

Building an application using design patterns and ...

I am writing this article to demonstrate how to build an application from scratch using SOLID principles and common design patterns; Author: Jon Woo ...
Read more

Applying Design Patterns to Solve Design Issues ...

Applying Design Patterns to Solve Design ... Design Patterns exist as a means to capture best practices and to pass along time-tested solutions to ...
Read more