Presentation is loading. Please wait.

Presentation is loading. Please wait.

Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College

Similar presentations


Presentation on theme: "Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College"— Presentation transcript:

1 Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College http://exciton.cs.oberlin.edu/research/sigcse99

2 What’s a “List”? ûSo many authors, so many definitions! XJava 1.2 Foundation Classes (JFC)Java 1.2 Foundation Classes (JFC) XGoFGoF ûCommon ground: Xa minimal and complete set of methods

3 Teaching Recursion ûTraditional Approach XOrdered Insert exampleOrdered Insert example XAlways having to check for the state of the system -- what a pain! XLook-ahead - what a mess! XSwapping -- how confusing! ûHave my goals been lost in the code specifics?

4 Back To Basics...

5 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!

6 State Pattern Implementation LRStruct > _state NullCase All requests delegated to the state. NullCase NonNullCase _first : Object _rest AState > Requests handled by polymorphism!

7 Invariants vs. Variants ûInvariant Behaviors: XIntrinsic to the definition of the data structure. XComplete and minimal set. ûVariant Behaviors: XExtrinsic algorithms operating on the structure. XComposed of invariant methods.

8 Basic Instinct ûKISS X“Keep it simple and no simpler.” ûBasic behavior of a LRS XExpose the components of its structure for the client to manipulate.

9 Invariant Behaviors û XgetFirst: Object // “car” XinsertAsFirst (dat: Object) // “cons” XgetRest: LRStruct // “cdr” û XsetFirst (dat: Object) XremoveFirst: Object XsetRest (tail: LRStruct) Functional: Imperative:

10 Big Deal!

11 ûA LRS should be able to execute any algorithm, past, present, and future, that operates on its structure, without changing any existing code. Xexecute(algo: IAlgo, input: Object):Object Added Intelligence an algorithm is an object! What kind of object is an algorithm?

12 Algorithm to insert input into an Ordered LRS ûLRS is in Null Case Xinsert input as first in the LRS ûLRS is in Non-Null Case XIf first element < input äinsert input as first in the LRS XElse ä recurse on the rest of the LRS base case/non-base case coding pattern!

13 Algorithm Interface Non-null case handler. Null case handler. Who determines which method is invoked?

14 Algorithm Interface Non-null case handler. Null case handler. Who determines which method is invoked?

15 “Visiting” the State Pattern _state return _state.execute(algo, this, param); return algo.nullCase(host, param); _rest return algo.nonNullCase(host, param); Request delegated to Flow Control via polymorphism Each state calls the appropriate “visiting” method

16 The Visitor Design Pattern - Invariant behaviors-Variant behaviors - Consistent “hook” for visitors - Fixed invocation interface HostsVisitors A system of cooperating objects: - Separate method for each host - Call only the desired method in the visitor

17 Ordered Insertion Abstraction ûNull Case XInsert here! ûNon-Null Case XIf local value < input äinsert here XElse ärecurse on the rest Seems simple enough...

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

19 Traditional Implementation ûIf empty Xmake new list with value ûElse XIf value <input äput value in temp äput input in value äif next node empty attach temp as rest äElse attach next node as temp’s rest attach temp as this node’s rest. XElse äif next node empty put input in temp attach temp as rest äelse recurse on the rest Hey! What happened to my simple abstraction???

20 Ordered Insertion Abstraction ûNull Case XInsert here! ûNon-Null Case XIf local value < input äinsert here XElse ärecurse on the rest Let’s try this again...

21 ûnullCase(host, input) Xhost.insertAsFirst(input) ûnonNullCase(host, input) Xif (host.getFirst() < input) ähost.insertAsFirst(input) Xelse ä(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.

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

23 Patterns and Pedagogy ûFundamental thinking tools of CS XAbstraction XRecursion ûSoftware engineering goals XReusability XExtensibility XRobustness

24 Conclusions ûThe 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.

25 Further Information ûhttp://exciton.cs.oberlin.edu/research/sigcse99


Download ppt "Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College"

Similar presentations


Ads by Google