Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using the Divide & Conquer Strategy to Teach Java Framework Design Conrad Cunningham, Yi Liu University of Mississippi Cuihua Zhang Northwest Vista College.

Similar presentations


Presentation on theme: "Using the Divide & Conquer Strategy to Teach Java Framework Design Conrad Cunningham, Yi Liu University of Mississippi Cuihua Zhang Northwest Vista College."— Presentation transcript:

1 Using the Divide & Conquer Strategy to Teach Java Framework Design Conrad Cunningham, Yi Liu University of Mississippi Cuihua Zhang Northwest Vista College

2 2 Purpose Use a familiar topic such as divide and conquer to introduce students to more alien-sounding concepts Use a familiar topic such as divide and conquer to introduce students to more alien-sounding concepts –Program families –Frameworks –Design patterns

3 3 Program Family Set of programs –sharing many common properties –worthwhile to study (and design) as a group David Parnas: “If you are developing a family of programs, you must do that consciously, or you will incur unnecessary long-term costs.”

4 4 Framework Generic application allowing creation of specific members of family Generic application allowing creation of specific members of family In OOP, framework described by In OOP, framework described by –set of abstract classes –way that instances of those classes collaborate Family members Family members –share same abstract classes –differ in implementations of concrete classes

5 5 Design Pattern Well-established solutions to recurring program design problems Well-established solutions to recurring program design problems

6 6 Framework Design Identify common aspects (frozen spots) Identify common aspects (frozen spots) –represent by set of ABSTRACT classes that collaborate in some structure –implement common behaviors by concrete template methods in base class Identify variable aspects (hot spots) Identify variable aspects (hot spots) –represent by abstract hook methods in base class –realize by concrete methods in an application of the family

7 7 Framework Construction Principles Unification Unification –use inheritance to implement hotspot subsystem Separation Separation –use delegation to implement hotspot subsystem

8 8 Template Method Pattern Unification principle AbstractClass TemplateMethods HookMethods ConcreteClass HookMethods

9 9 Strategy Pattern Separation principle Strategy HookMethods ConcreteStrategyB HookMethods ConcreteStrategyC HookMethods ConcreteStrategyA HookMethods Context TemplateMethods strategy

10 10 Divide & Conquer Approach Divide original problem into subproblems of same type Divide original problem into subproblems of same type Solve each subproblem independently Solve each subproblem independently Combine subproblem solutions into solution for original problem Combine subproblem solutions into solution for original problem

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

12 12 Example: Mergesort

13 13 Divide & Conquer Pseudocode function solve(p) {if isSimple(p) return simplySolve(p); else{ Problem [] sp = decompose(p); for (i = 0; i

14 14 Divide & Conquer Functions template method: template method: –solve() hook methods: hook methods: –isSimple() –simplySolve() –decompose() –combine()

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

16 16 Template Method Class abstract public class DivConqTemplate {final public Solution solve(Problem p) // takes Problem description {Problem[] pp; // and returns Solution description {Problem[] pp; // and returns Solution description 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); }

17 17 Framework Application: Quicksort Divide and conquer task: sort a sequence in place Divide and conquer task: sort a sequence in place Mapping problem into divide and conquer framework 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 Describing the problem and solution –identify sequence of values –identify beginning and ending elements of segment

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

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

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

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

22 22 Quicksort Subclass (3 of 3) protected Solution combine (Problem p, Solution[] ss) protected Solution combine (Problem p, Solution[] ss) {return (Solution) p; } {return (Solution) p; } private void swap (int [] a, int first, int last) private void swap (int [] a, int first, int last) {int temp = a[first]; {int temp = a[first]; a[first] = a[last]; a[last] = temp; } }}

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

24 24 To Use a Framework Implement concrete hook methods Implement concrete hook methods Plug subsystems into hot spots Plug subsystems into hot spots

25 25 Educational Observations Students should learn to design program families Students should learn to design program families Students should be explicitly taught appropriate design and programming techniques Students should be explicitly taught appropriate design and programming techniques Frameworks are understandable by students familiar with OOP Frameworks are understandable by students familiar with OOP Divide and Conquer algorithmic strategy provides a familiar, understandable environment for learning 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 Other exercises are needed to teach students to identify and design appropriate hot spots

26 26 Summary Targeted at Java programming or software design courses Targeted at Java programming or software design courses Provides a simple example to teach computing students design and use of frameworks Provides a simple example to teach computing students design and use of frameworks Aimed at improving students’ abilities to construct and use abstractions in design of program families Aimed at improving students’ abilities to construct and use abstractions in design of program families

27 27 Acknowledgements Acxiom Corporation grant Acxiom Corporation grant –“Acxiom Laboratory for Software Architecture and Component Engineering (ALSACE)” Students in ENGR 660 in Fall 2003 Students in ENGR 660 in Fall 2003


Download ppt "Using the Divide & Conquer Strategy to Teach Java Framework Design Conrad Cunningham, Yi Liu University of Mississippi Cuihua Zhang Northwest Vista College."

Similar presentations


Ads by Google