1 / 55

Teaching Java Framework Design Using Classic Problems

Teaching Java Framework Design Using Classic Problems. H. Conrad Cunningham 1 Yi Liu 2 Cuihua Zhang 3 1 University of Mississippi 2 South Dakota State University 3 Northeast Lakeview College. Outline. Motivation Software families Foundational concepts Framework construction

addo
Download Presentation

Teaching Java Framework Design Using Classic Problems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Teaching Java Framework Design Using Classic Problems H. Conrad Cunningham 1 Yi Liu 2 Cuihua Zhang 3 1 University of Mississippi 2 South Dakota State University 3 Northeast Lakeview College

  2. Outline • Motivation • Software families • Foundational concepts • Framework construction • Divide and conquer case study • Observations • Framework generalization using function generalization • Binary tree traversal case study • Conclusions

  3. Technical Motivation Software • becoming more pervasive • increasing in size and complexity • expected to provide product flexibility Therefore, need to increase productivity, manage complexity, and provide flexibility to systems builders

  4. Competitive Motivation “Heads down” programming expertise has become commodity skill available globally Therefore, our students need to • become skilled domain analysts • learn to adapt software to local needs • master higher level concepts and skills

  5. One Response: Focus on Software Families Software family is set of programs • Sharing many common properties • Worthwhile to study as a group before each individually • Known as software frameworks or product lines David Parnas: “If you are developing a family of programs, you must do that consciously, or you will incur unnecessary long-term costs.”

  6. How Much Progress? • In 1976, Parnas published the seminal article “On the Design and Development of Program Families.” • In 2001, Parnas wrote: “Although there is now growing … interest and some evidence of … success in applying the idea, the majority of industrial programmers seem to ignore it in their rush to produce code.”

  7. Our Viewpoint Students should: • Learn concepts and methods for designing software families • Learn technologies for designing and implementing software family as framework Teachers can: • Use familiar problems initially to introduce the new concepts and technologies

  8. Foundational Concepts • Classic concepts (Parnas) • information hiding modules • abstract interfaces • Object-oriented programming • inheritance • polymorphism • Design patterns

  9. Information-Hiding Modules Module • Work assignment given to a programmer or group of programmers • Good characteristics: independent development, comprehensible, changeable Information hiding • Design modules around assumptions likely to change • Hide a design decision within a module • as its secret

  10. Abstract Interfaces Interface • Set of assumptions programmer must make about another module to demonstrate correctness of his module • not just syntax, also semantics Abstract interface • Module interface that does not change when one module implementation is substituted for another

  11. Design Patterns • Well-established solutions to recurring design problems • E.g. Template Method, Strategy, Composite, Visitor, Singleton

  12. Software Framework • Generic application allowing creation of members of family of related programs • Reusable design expressed as set of abstract classes and way they collaborate • Common and variable aspects known as frozen spots and hot spots Frozen spots Framework library Framework Hot spots User-supplied code(application specific)

  13. Hot Spot Subsystem • Template methoddefines a common behavior for family • part of frozen spot implementation • Template method calls hook methods to invoke behaviors specific to family member • hook methods form hot spot subsystem • set of hook methods form abstract interface to information hiding module

  14. Framework Construction Principles • Unification • Use inheritance to implement hot spot subsystem • Separation • Use delegation to implement hot spot subsystem

  15. AbstractClass TemplateMethods HookMethods ConcreteClass HookMethods Unification Principle Template Method pattern

  16. Context TemplateMethods Strategy HookMethods strategy ConcreteStrategyA HookMethods ConcreteStrategyB HookMethods ConcreteStrategyC HookMethods Separation Principle Strategy pattern

  17. Example:Divide and Conquer Algorithms • Divide original problem into subproblems of same type • Solve each subproblem independently • Combine subproblem solutions into solution for original problem What are some examples?

  18. Well-Known Examples • Mergesort • Quicksort • Heapify • Binary search • Fast matrix multiplication • Fast Fourier Transform (FFT)

  19. Example: Mergesort 1 12 10 6 1 12 10 6 12 10 6 1 6 10 1 12 1 6 10 12

  20. Divide and Conquer Pseudocode function solve(p) { if isSimple(p) return simplySolve(p); else { Problem [] sp = decompose(p); for (i = 0; i<sp.length; i = i+1) sol[i] = solve(sp[i]); return combine (sol); } }

  21. Divide & Conquer Functions • template method: • solve() • hook methods: • isSimple() • simplySolve() • decompose() • combine() • other: problem and solution descriptions

  22. DivConqTemplate final void solve() Divide and Conquer abstract isSimple() abstract simplySolve() abstract decompose() abstract combine() QuickSort MergeSort BinarySearch isSimple() isSimple() isSimple() simplySolve() simplySolve() simplySolve() decompose() decompose() decompose() combine() combine() combine() Framework: Template Method Pattern

  23. Template Method Class abstract public class DivConqTemplate { final public Solutionsolve(Problem p) { Problem[] pp; if (isSimple(p)) { return simplySolve(p); } else { pp = decompose(p); } Solution[] ss = new Solution[pp.length]; for(int i=0; i < pp.length; i++) { ss[i] = solve(pp[i]); } return combine(p,ss); } abstract protected boolean isSimple (Problem p); abstract protected Solution simplySolve (Problem p); abstract protected Problem[] decompose (Problem p); abstract protected Solution combine(Problem p, Solution[] ss); }

  24. Framework Application: Quicksort • Divide and conquer task: sort a sequence in place • Mapping problem into divide and conquer framework • decompose: partition into two segments about a pivot value (rearranges values in array) • recursively sort each segment until just one element (isSimple and simplySolve) • combine: values already in needed location • Describing the problem and solution • identify sequence of values • identify beginning and ending elements of segment

  25. Problem and Solution Description Simplifying assumption: sort integer arrays • Problem description for sort • overall array • beginning and ending indices of segment • Solution description for sort • same as Problem description • except array values rearranged in place • QuickSortDesc to represent both

  26. QuickSortDesc Class public class QuickSortDesc implements Problem, Solution { public QuickSortDesc (int[] arr, int first, int last) { this.arr = arr; this.first = first; this.last = last; } public int getFirst () { return first; } public int getLast () { return last; } public int[] getArr () { return arr; } private int[] arr; private int first, last; }

  27. Quicksort Subclass (1 of 3) public class QuickSort extends DivConqTemplate { protected boolean isSimple (Problem p) { return ( ((QuickSortDesc)p).getFirst() >= ((QuickSortDesc)p).getLast() ); } protected Solution simplySolve (Problem p) { return (Solution) p ; }

  28. Quicksort Subclass (2 of 3) protected Problem[] decompose (Problem p) { int first = ((QuickSortDesc)p).getFirst(); int last = ((QuickSortDesc)p).getLast(); int[] a = ((QuickSortDesc)p).getArr (); int x = a[first]; // pivot value int sp = first; for (int i = first + 1; i <= last; i++) { if (a[i] < x) { swap (a, ++sp, i); } } swap (a, first, sp); Problem[] ps = new QuickSortDesc[2]; ps[0] = new QuickSortDesc(a,first,sp-1); ps[1] = new QuickSortDesc(a,sp+1,last); return ps; }

  29. Quicksort Subclass (3 of 3) protected Solution combine (Problem p, Solution[] ss) { return (Solution) p; } // should make sure adj private void swap (int [] a, int first, int last) { int temp = a[first]; a[first] = a[last]; a[last] = temp; } }

  30. To Build a Framework • Represent common aspects as abstract classes and concrete template methods • Represent variable aspects as abstract hook methods and some concrete hook methods in hot spot subsystems • Organize using unification or separation principle

  31. To Use a Framework • Implement concrete hook methods • Plug subsystems into hot spots

  32. Educational Observations • Students should learn to design program families • Students should be explicitly taught appropriate design and programming techniques • Frameworks are understandable by students familiar with OOP • Divide and Conquer algorithmic strategy provides a familiar, understandable environment for learning • Other exercises are needed to teach students to identify and design appropriate hot spots

  33. Framework Generalization • Nontrivial to identify needed hot spot abstractions • Difficult to specify hot spot behaviors • Need systematic generalization methodology • Explore function generalization • incrementally generalize functional structure of specification to produce general application

  34. 21 18 50 Preorder 21 18 3 1 20 50 30 3 1 20 30 Binary Tree Traversal procedure preorder(t) { if t null, then return; perform visit action for root of tree t; preorder (left subtree of t); preorder (right subtree of t); }

  35. Function Generalization • Create a prototype of one family member • Define scope of family • Identify frozen spots and hot spots • Analyze and design each hot spot system • generalize program for hot spot • transform simple function to template method with hook calls

  36. Composite Pattern for Structure Component BinTree abstract getValue( ) abstract getLeft( ) abstract getright( ) * abstract setValue( ) abstract setLeft( ) anstract setRight( ) Composite Leaf Node Nil 1

  37. Binary Tree Component Base Class:Preorder Traversal abstract public class BinTree { public void setValue(Object v) { } // mutators public void setLeft(BinTree l) { } // default public void setRight(BinTree r){ } abstract public void preorder(); public Object getValue() { return null; } // accessors public BinTree getLeft() { return null; } // default public BinTree getRight(){ return null; } }

  38. Binary Tree Composite Node Class:Preorder Traversal public classNode extends BinTree { public Node(Object v, BinTree l, BinTree r) { value = v; left = l; right = r; } public void setValue(Object v) { value = v; } public void setLeft(BinTree l) { left = l; } public void setRight(BinTree r) { right = r; } public void preorder() // traversal { System.out.println("Visit node with value “ + value); left.preorder(); right.preorder(); } public Object getValue() { return value; } public BinTree getLeft() { return left; } public BinTree getRight() { return right; } private Object value; private BinTree left, right; }

  39. Binary Tree Leaf Node Class:Preorder Traversal public class Nil extends BinTree { private Nil(){ } // require use of getNil() public void preorder(){ }; // traversal static public void getNil() // Singleton { return theNil; } static private BinTree theNil = new Nil(); }

  40. Framework Scope • Standard kinds of depth-first traversals • Flexible visit actions that are functions of accumulated state along traversal • Other traversals orders (e.g. level by level) • Binary trees, but not multiway trees or graphs What are the frozen spots and the hot spots?

  41. Frozen Spots • Structure of tree not redefined by clients • Traversal accesses every node once (unless stopped early) • Traversal performs one or more visit actions on access to node of tree Represented by a traversal method

  42. Hot Spots • Variability in the visit operation’s action • Variability in ordering of visit action with respect to subtree visits • Variability in tree navigation technique (not just left-to-right, depth first) Represented as additional functions, types, and class definitions to traversal function

  43. Hot Spot #1:Generalizing the Visit Action • Represent visit action by state-updating Strategy object passed into traversal function • Accumulate state along traversal path abstract public class BinTree { ... abstract public Object preorder (Object ts, PreorderStrategy v); ... } public interface PreorderStrategy { abstract public Object visitPre(Object ts, BinTree t); }

  44. Hot Spot #1:Generalizing the Visit Action public class Node extends BinTree { ... public Object preorder (Object ts, PreorderStrategy v) { ts = v.visitPre(ts, this); ts = left.preorder(ts, v); ts = right.preorder(ts, v); return ts; } ... } public class Nil extends BinTree { ... public Object preorder(Object ts, PreorderStrategy v) { return ts; } ... }

  45. Hot Spot #2:Generalizing the Visit Order • Allow visit actions at first arrival (left), between subtree traversals (bottom), and before final departure (right) -- Euler tree traversal abstract public class BinTree { ... abstract public Object traverse (Object ts, EulerStrategy v) ; ... } public interface EulerStrategy { abstract public Object visitLeft(Object ts,BinTree t); abstract public Object visitBottom(Object ts,BinTree t); abstract public Object visitRight(Object ts,BinTree t); abstract public Object visitNil(Object ts,BinTree t); }

  46. Hot Spot #2:Generalizing the Visit Order public class Node extends BinTree { ... public Object traverse (Object ts, EulerStrategy v) { ts = v.visitLeft(ts,this); // arrival from above ts = left.traverse(ts,v); ts = v.visitBottom(ts,this); // return from below ts = right.traverse(ts,v); ts = v.visitRight(ts,this); // completion return ts; } ... } public class Nil extends BinTree { ... public Object traverse (Object ts, EulerStrategy v) { return v.visitNil(ts,this); } ... }

  47. Client Visitor <<abstract Interface>> BinTreeVisitor BinTreeVisitorTest abstract visit( ) Concrete Visitor Concrete Visitor BreadthFirstVisitor EulerTourVisitor MappingVisitor BinTree (Element) abstract accept( ) abstract getValue( ) uses abstract getLeft( ) abstract getRight( ) abstract setValue( ) abstract setLeft( ) Concrete abstract setRight( ) Element Node Nil Hot Spot #3:Generalizing the Tree Navigation Visitor pattern

  48. Hot Spot #3:Generalizing the Tree Navigation abstract public class BinTree { ... abstract public void accept (BinTreeVisitor v); // accept Visitor v // e.g., v.visit(this) ... } public interface BinTreeVisitor { abstract void visit (Node t); // overloaded abstract void visit (Nil t); // method name }

  49. Hot Spot #3:Euler Tour Traversal as Special Case public class EulerTourVisitor implements BinTreeVisitor { public EulerTourVisitor(EulerStrategy es, Object ts) { this.es = es; this.ts = ts; } public void setVisitStrategy(EulerStrategy es) { this.es = es; } public void setResult(Object r) { ts = r; } public void visit(Node t) // Visitor hooks { ts = es.visitLeft(ts,t); // arrival from above t.getLeft().accept(this); ts = es.visitBottom(ts,t); // return from below t.getRight().accept(this); ts = es.visitRight(ts,t); // completion } public void visit(Nil t) { ts = es.visitNil(ts,t); } public Object getResult(){ return ts; } // accessor private EulerStrategy es; // state changing ops private Object ts; // traversal state }

  50. Generalization Recap • Used function generalization to construct framework systematically and incrementally • Produced an executable program prototype at each transformation • Defined appropriate hooks (hot spot abstractions) at each step • Constructed general binary tree traversal program with better understanding of supported hot spot behaviors

More Related