SlideShare une entreprise Scribd logo
1  sur  9
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/

Contenu connexe

Similaire à Applying design patterns

Amcat automata questions
Amcat   automata questionsAmcat   automata questions
Amcat automata questions
ESWARANM92
 

Similaire à Applying design patterns (20)

module5_backtrackingnbranchnbound_2022.pdf
module5_backtrackingnbranchnbound_2022.pdfmodule5_backtrackingnbranchnbound_2022.pdf
module5_backtrackingnbranchnbound_2022.pdf
 
22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf
 
Sudoku Solver
Sudoku SolverSudoku Solver
Sudoku Solver
 
Ruby basics ||
Ruby basics ||Ruby basics ||
Ruby basics ||
 
2CPP16 - STL
2CPP16 - STL2CPP16 - STL
2CPP16 - STL
 
Fuzzy logic
Fuzzy logicFuzzy logic
Fuzzy logic
 
Equations and tilings
Equations and tilingsEquations and tilings
Equations and tilings
 
Multilayer Perceptron Neural Network MLP
Multilayer Perceptron Neural Network MLPMultilayer Perceptron Neural Network MLP
Multilayer Perceptron Neural Network MLP
 
Amcat automata questions
Amcat   automata questionsAmcat   automata questions
Amcat automata questions
 
Amcat automata questions
Amcat   automata questionsAmcat   automata questions
Amcat automata questions
 
Java Programming Comprehensive Guide.pptx
Java Programming Comprehensive Guide.pptxJava Programming Comprehensive Guide.pptx
Java Programming Comprehensive Guide.pptx
 
Kakuro: Solving the Constraint Satisfaction Problem
Kakuro: Solving the Constraint Satisfaction ProblemKakuro: Solving the Constraint Satisfaction Problem
Kakuro: Solving the Constraint Satisfaction Problem
 
Mathematics online: some common algorithms
Mathematics online: some common algorithmsMathematics online: some common algorithms
Mathematics online: some common algorithms
 
Java 103 intro to java data structures
Java 103   intro to java data structuresJava 103   intro to java data structures
Java 103 intro to java data structures
 
Stoop 300-block optimizationinvw
Stoop 300-block optimizationinvwStoop 300-block optimizationinvw
Stoop 300-block optimizationinvw
 
03 Single layer Perception Classifier
03 Single layer Perception Classifier03 Single layer Perception Classifier
03 Single layer Perception Classifier
 
ann-ics320Part4.ppt
ann-ics320Part4.pptann-ics320Part4.ppt
ann-ics320Part4.ppt
 
ann-ics320Part4.ppt
ann-ics320Part4.pptann-ics320Part4.ppt
ann-ics320Part4.ppt
 
Sess07 Clustering02_KohonenNet.pptx
Sess07 Clustering02_KohonenNet.pptxSess07 Clustering02_KohonenNet.pptx
Sess07 Clustering02_KohonenNet.pptx
 
Paper study: Learning to solve circuit sat
Paper study: Learning to solve circuit satPaper study: Learning to solve circuit sat
Paper study: Learning to solve circuit sat
 

Dernier

%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 

Dernier (20)

Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 

Applying design patterns

  • 4. Applying Patterns • Break the problem down to common known problems • Apply Known Solutions
  • 5. 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?
  • 6. 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
  • 7. 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)); } } }
  • 8. 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.
  • 9. 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/