Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University...
-
Upload
marsha-burns -
Category
Documents
-
view
228 -
download
0
description
Transcript of Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University...
![Page 1: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/1.jpg)
Patterns for DecouplingData Structures and AlgorithmsDung “Zung” Nguyen
Pepperdine University/University of Houston
Stephen Wong Oberlin College
http://exciton.cs.oberlin.edu/research/sigcse99
![Page 2: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/2.jpg)
What’s a “List”?So many authors, so many
definitions! Java 1.2 Foundation Classes (JFC) GoF
Common ground: a minimal and complete set of
methods
![Page 3: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/3.jpg)
Teaching RecursionTraditional Approach
Ordered Insert example Always having to check for the
state of the system -- what a pain! Look-ahead - what a mess! Swapping -- how confusing!
Have my goals been lost in the code specifics?
![Page 4: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/4.jpg)
Back To Basics...
![Page 5: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/5.jpg)
Linear Recursive Structure
A linear recursive structure (LRS) can be empty or non-empty.
If a LRS is empty, it contains no element.
If it is not empty, it contains an element called first, and a LRS object called rest.
A LRS is a 2-state object!
![Page 6: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/6.jpg)
State Pattern Implementation
LRStruct<<context>>
_state
NullCase
All requests delegated to the state.
NullCase
NonNullCase_first : Object
_rest
AState<<abstract state>>
Requests handled by polymorphism!
![Page 7: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/7.jpg)
Invariants vs. Variants
Invariant Behaviors: Intrinsic to the definition of the
data structure. Complete and minimal set.
Variant Behaviors: Extrinsic algorithms operating on
the structure. Composed of invariant methods.
![Page 8: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/8.jpg)
Basic Instinct
KISS“Keep it simple and no simpler.”
Basic behavior of a LRSExpose the components of its structure for the client to manipulate.
![Page 9: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/9.jpg)
Invariant Behaviors
getFirst: Object // “car” insertAsFirst (dat: Object) // “cons” getRest: LRStruct // “cdr”
setFirst (dat: Object) removeFirst: Object setRest (tail: LRStruct)
Functional:
Imperative:
![Page 10: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/10.jpg)
Big Deal!
![Page 11: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/11.jpg)
A LRS should be able to execute any algorithm, past, present, and future, that operates on its structure, without changing any existing code.
execute(algo: IAlgo, input: Object):Object
Added Intelligence
an algorithm is an object!
What kind of object is an algorithm?
![Page 12: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/12.jpg)
Algorithm to insert input into an Ordered LRS
LRS is in Null Case insert input as first in the LRS
LRS is in Non-Null Case If first element < input
insert input as first in the LRS Else
recurse on the rest of the LRS
base case/non-base case coding pattern!
![Page 13: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/13.jpg)
Algorithm Interface
IAlgo+nullCa se(host: LRStruct, par am: Object) : Object+nonNullCa se(host: LRStr uct, para m: Object) : Object
«Interface»
Algo1+nullCase(host: LRStruct, param: Object) : Object+nonNullCas e(host: LRStr uct, param: Object ) : Object
«concrete algorithm»Algo2
«concrete algorithm»
+nullCase(host: LRStruct, param: Object) : Object+nonNullCas e(host: LRStr uct, param: Object ) : Object
IAlgo+nullCase(host: LRStruct, pa ram: Object ) : Object+nonNullCa se(host: LRStruct, pa ram: Object) : Object
Algo1+nullCase(host: LRStruct, param : Object) : Object+nonNullCase(host: LRStruct, param : Object) : Object
«concrete algorithm»Algo2
«concrete algorithm»
+nullCase(host: LRStruct, param : Object) : Object+nonNullCase(host: LRStruct, param : Object) : Object
IAlgo+nullCase(host: LRStruct, pa ram: Object ) : Object+nonNullCa se(host: LRStruct, pa ram: Object) : Object
Algo1+nullCase(host: LRStruct, param : Object) : Object+nonNullCase(host: LRStruct, param : Object) : Object
«concrete algorithm»Algo2
«concrete algorithm»
+nullCase(host : LRStruct, param : Object) : Object+nonNullCase(host: LRStruct, param : Object) : Object
Non-null case handler.
Null case handler.
Who determines which method is invoked?
![Page 14: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/14.jpg)
Algorithm Interface
Non-null case handler.
Null case handler.
Who determines which method is invoked?
IAlgo+nullCa se(host: LRStruct, par am: Object) : Object+nonNullCa se(host: LRStr uct, para m: Object) : Object
«Interface»
Algo1+nullCase(host: LRStruct, param: Object) : Object+nonNullCas e(host: LRStr uct, param: Object ) : Object
«concrete algorithm»Algo2
«concrete algorithm»
+nullCase(host: LRStruct, param: Object) : Object+nonNullCas e(host: LRStr uct, param: Object ) : Object
![Page 15: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/15.jpg)
“Visiting” the State Pattern
_stateLRStruct+e xecute(algo: IAlgo, param : Object) : Object
return _state.execute(algo, this, param);
«host»
AStateexecute(algo: IAlgo, host: LRStruct, para m: O bject) : Object
«abstract state»_state
NonNullCase«concrete state»
_rest
return _state.execute(algo, this, param);
AStateexecute(algo: IAlgo, host : LRStruct, pa ram: Obj ect) : Obj ect
NullCaseexecute(algo: IAlgo, host : LRStruct, param: Obje ct) : Obje ct
«concrete state»
return algo.nullCase(host, param);
NonNullCase«concrete state»
execute(algo: IAlgo, host : LRStruct, param: Obje ct) : Obje ct
return algo.nonNullCase(host, param);
-Object: _firs t
return algo.nullCase(host, param);
_rest
+e xecute(algo: IAlgo, par am: Obje ct) : ObjectAState
execute(algo: IAlgo, host : LRStruct, pa ram: Obj ect) : Obj ect
NullCaseexecute(algo: IAlgo, host : LRStruct, param: Obje ct) : Obje ct
«concrete state»
return algo.nullCase(host, param);
NonNullCase«concrete state»
execute(algo: IAlgo, host : LRStruct, param: Obje ct) : Obje ct
return algo.nonNullCase(host, param);
-Object: _firs t
_rest
return algo.nonNullCase(host, param);
LRStruct+execute(algo: IAlgo, param: Obj ect) : Object
return _state.execute(algo, this, param);
«host»
AStateexecute(algo: IAlgo, host: LRStruct, param: Object) : Object
«abstract state»_state
NullCase«concrete state»
NonNullCase«concrete state»
Request delegated to
Flow Control via polymorphism
Each state calls the appropriate “visiting” method
![Page 16: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/16.jpg)
The Visitor Design Pattern
- Invariant behaviors -Variant behaviors- Consistent “hook” for visitors
- Fixed invocation interface
Hosts VisitorsA system of cooperating objects:
- Separate method for each host
- Call only the desired method in the visitor
![Page 17: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/17.jpg)
Ordered Insertion Abstraction
Null Case Insert here!
Non-Null Case If local value < input
insert here Else
recurse on the restSeems simple enough...
![Page 18: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/18.jpg)
The Drive Towards Abstraction
Key to robust, reusable code.Key to solving new problems.Key to putting different problems
into proper perspectives. Teach students to look for the
abstraction of the problem from the very start.
![Page 19: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/19.jpg)
Traditional Implementation
If empty make new list
with valueElse
If value <inputput value in temp put input in valueif next node empty
• attach temp as rest
Else• attach next node as
temp’s rest• attach temp as this
node’s rest. Else
if next node empty• put input in temp• attach temp as rest
else• recurse on the rest
Hey! What happened to my simple abstraction???
![Page 20: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/20.jpg)
Ordered Insertion Abstraction
Null Case Insert here!
Non-Null Case If local value < input
insert here Else
recurse on the rest
Let’s try this again...
![Page 21: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/21.jpg)
nullCase(host, input) host.insertAsFirst(input)
nonNullCase(host, input) if (host.getFirst() < input)
host.insertAsFirst(input) else
(host.getRest()).execute(this, input)
State+Visitor Implementation
Such magic when the implementation matches the abstraction!
No Flow Control!The LRS host
determines what happens and when.
![Page 22: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/22.jpg)
State+Visitor Lists
Demonstrate the power of a close match between an abstraction and its implementation.
Enable students to concentrate on the fundamentals of data and algorithm abstraction
Enable students to easily isolate the key issues in recursion.
![Page 23: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/23.jpg)
Patterns and Pedagogy
Fundamental thinking tools of CS Abstraction Recursion
Software engineering goals Reusability Extensibility Robustness
![Page 24: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/24.jpg)
ConclusionsThe state pattern properly implements
the abstraction of a list.The abstraction of the algorithm-list
relationship leads to the visitor pattern.
Data structures are naturally decoupled from their algorithms. This can be expressed using the state+visitor pattern.
![Page 25: Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College](https://reader033.fdocuments.net/reader033/viewer/2022051101/5a4d1b027f8b9ab059987007/html5/thumbnails/25.jpg)
Further Information http://exciton.cs.oberlin.edu/research/
sigcse99