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

Slides:



Advertisements
Similar presentations
SIGCSE 2005, St. Louis, MO Design Patterns for Recursive Descent Parsing Dung Nguyen, Mathias Ricken & Stephen Wong Rice University.
Advertisements

Marine Biology Simulation Part I: Introduction Dung “Zung” Nguyen Mathias Ricken Stephen Wong TeachJava 2004! June 25, 2004.
Design Patterns for Self-Balancing Trees Dung “Zung” Nguyen Stephen Wong Rice University.
Design Patterns for Marine Biology Simulation Dung “Zung” Nguyen Mathias Ricken Stephen Wong Rice University.
A list = a Linear Recursive Structure (LRS or LRStruct) What is a list? 1.the empty list is a list 2.a pair whose tail is a list is itself a list This.
Preliminaries Attendance sheets –I remembered! Survey links –HW1 time survey –Anonymous feedback survey HW discussion (4PM, Commons 9)
Talk tonight Richard Stallman Norton 112 6:00 PM.
Preliminaries Stage 1 due tonight by 9:00 PM HW1 discussion (4PM, Commons 9) –may try for next week? HW2 available later today, due Monday by 9:00 PM.
No homework this week Stage 2 starts next week. Code review Team with N members is assigned N submissions to review Discuss submissions within team Everyone.
How do visitors work? This set of slides traces through the execution of a visitor we’ve seen before (the lengthVisitor) on a short list. It shows how.
Design Patterns In OPM Presented by: Galia Shlezinger Instructors: Prop. Dov Dori, Dr. Iris Berger.
Lists We’ve seen an array-based list implementation, the ArrayList. Advantage of an array-based implementation: –fast access to a specific index –typically.
Lists We’ve seen an array-based list implementation, the ArrayList.
Chapter 3: Arrays, Linked Lists, and Recursion
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
Composite Design Pattern. Motivation – Dynamic Structure.
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
Case Studies on Design Patterns Design Refinements Examples.
Introduction. 2COMPSCI Computer Science Fundamentals.
Design Patterns for Self-Balancing Trees Dung “Zung” Nguyen Stephen Wong Rice University.
OOP in Introductory CS Stephen Wong and “Zung” Nguyen Rice University Better students though abstraction.
Design Patterns for Sorting Teaching something old in a new light Dung “Zung” Nguyen Stephen Wong Rice University.
Chapter 10 Analysis and Design Discipline. 2 Purpose The purpose is to translate the requirements into a specification that describes how to implement.
AOP-1 Aspect Oriented Programming. AOP-2 Aspects of AOP and Related Tools Limitation of OO Separation of Concerns Aspect Oriented programming AspectJ.
GoF Sections Design Problems and Design Patterns.
Chapter 8 Object Design Reuse and Patterns. Object Design Object design is the process of adding details to the requirements analysis and making implementation.
Design Patterns for Marine Biology Simulation Dung “Zung” Nguyen Mathias Ricken Stephen Wong Rice University.
Structural Design Patterns
Nifty Assignments: Marine Biology Simulation OOPSLA ‘04 Educators’ Symposium October 25, 2004 Eric Cheng Mathias Ricken Dung “Zung” Nguyen Stephen Wong.
Frameworks CompSci 230 S Software Construction.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
DESIGN PATTERNS -BEHAVIORAL PATTERNS WATTANAPON G SUTTAPAK Software Engineering, School of Information Communication Technology, University of PHAYAO 1.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
The Visitor Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Patterns for Decoupling Data Structures and Algorithms or How visitors can help you grow! Stephen Wong, Oberlin College Dung “Zung” Nguyen, Pepperdine.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.
Design Patterns for Games Proceedings of the 33 rd SIGCSE technical symposium on Computer Science Education Melisa Tyira SE510.
Chapter 8 Object Design Reuse and Patterns. More Patterns Abstract Factory: Provide manufacturer independence Builder: Hide a complex creation process.
CSE 332: Design Patterns (Part II) Last Time: Part I, Familiar Design Patterns We’ve looked at patterns related to course material –Singleton: share a.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under.
Given a node v of a doubly linked list, we can easily insert a new node z immediately after v. Specifically, let w the be node following v. We execute.
Design Patterns Source: “Design Patterns”, Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides And Created.
Introduction to .NET Florin Olariu
MPCS – Advanced java Programming
Design Patterns for Sorting Teaching something old in a new light
Marine Biology Simulation Part I: Introduction
Factory Patterns 1.
Behavioral Design Patterns
Design Patterns for Sorting something old in a new light
CS 270 Math Foundations of CS Jeremy Johnson
Design Patterns for Self-Balancing Trees
Design Patterns for Self-Balancing Trees
Design Patterns for Sorting Teaching something old in a new light
Design Patterns for Self-Balancing Trees
Software Engineering Lecture #8.
Nifty Assignments: Marine Biology Simulation
Object Oriented Practices
Design Patterns for Lazy Evaluation
Design Patterns for Recursive Descent Parsing
Design Patterns for Self-Balancing Trees
Review CSE116 2/21/2019 B.Ramamurthy.
Linear Recursive Structures(LRS)
Introduction to Design Patterns
Introduction to Data Structure
Design Patterns for Self-Balancing Trees
Marine Biology Simulation Part II: Assignment, Milestone 1
Software Design Lecture : 39.
Presentation transcript:

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

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

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?

Back To Basics...

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!

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

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.

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.

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

Big Deal!

û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?

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!

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

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

“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

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

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

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.

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

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

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

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.

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

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.

Further Information û