Applying design patterns

50 %
50 %
Technology

Published on March 12, 2014

Author: chellash

Source: slideshare.net

Description

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

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/

 User name: Comment:

Related presentations

Neuquén y el Gobierno Abierto

October 30, 2014

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

Decision CAMP 2014 - Erik Marutian - Using rules-b...

October 16, 2014

In this presentation we will describe our experience developing with a highly dyna...

Schema.org: What It Means For You and Your Library

November 7, 2014

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

WearableTech: Una transformación social de los p...

November 3, 2014

Un recorrido por los cambios que nos generará el wearabletech en el futuro

O Impacto de Wearable Computers na vida das pessoa...

November 5, 2014

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

All you need to know about the Microsoft Band

November 6, 2014

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

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.

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

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

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

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 ...

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 ...

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 ...