Using the Divide - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Using the Divide

Description:

recursively sort each segment until just one element (isSimple and simplySolve) ... 'Acxiom Laboratory for Software Architecture and Component Engineering (ALSACE) ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 28
Provided by: stu1165
Category:

less

Transcript and Presenter's Notes

Title: Using the Divide


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

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

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
Framework
  • Generic application allowing creation of specific
    members of family
  • In OOP, framework described by
  • set of abstract classes
  • way that instances of those classes collaborate
  • Family members
  • share same abstract classes
  • differ in implementations of concrete classes

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

6
Framework Design
  • 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)
  • represent by abstract hook methods in base class
  • realize by concrete methods in an application of
    the family

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

8
Template Method Pattern
  • Unification principle

9
Strategy Pattern
  • Separation principle

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

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

12
Example Mergesort
1 12 10 6
1 12
10 6
12
10
6
1
6 10
1 12
1 6 10 12
13
Divide Conquer Pseudocode
  • function solve(p)
  • if isSimple(p)
  • return simplySolve(p)
  • else
  • Problem sp decompose(p)
  • for (i 0 iltsp.length i i 1)
  • soli solve(spi)
  • return combine (sol)

14
Divide Conquer Functions
  • template method
  • solve()
  • hook methods
  • isSimple()
  • simplySolve()
  • decompose()
  • combine()

15
Framework Template Method Pattern
16
Template Method Class
  • abstract public class DivConqTemplate
  • final public Solution solve(Problem p) //
    takes Problem description
  • Problem pp // and returns Solution
    description
  • if (isSimple(p)) return simplySolve(p)
  • else pp decompose(p)
  • Solution ss new Solutionpp.length
  • for(int i0 i lt pp.length i)
  • ssi solve(ppi)
  • 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
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

18
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 object to represent both

19
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
  • private int arr
  • private int first, last

20
Quicksort Subclass (1 of 3)
  • public class QuickSort extends DivConqTemplate
  • protected boolean isSimple (Problem p)
  • return ( ((QuickSortDesc)p).getFirst()
  • gt ((QuickSortDesc)p).getLast() )
  • protected Solution simplySolve (Problem p)
  • return (Solution) p

21
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 afirst // pivot value
  • int sp first
  • for (int i first 1 i lt last i)
  • if (ai lt x) swap (a, sp,
    i)
  • swap (a, first, sp)
  • Problem ps new QuickSortDesc2
  • ps0 new QuickSortDesc(a,first,sp-1)
  • ps1 new QuickSortDesc(a,sp1,last)
  • return ps

22
Quicksort Subclass (3 of 3)
  • protected Solution combine (Problem p,
    Solution ss)
  • return (Solution) p
  • private void swap (int a, int first, int
    last)
  • int temp afirst
  • afirst alast
  • alast temp

23
To Build a Framework
  • 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
  • Organize using unification or separation principle

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

25
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

26
Summary
  • Targeted at Java programming or software design
    courses
  • 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

27
Acknowledgements
  • Acxiom Corporation grant
  • Acxiom Laboratory for Software Architecture and
    Component Engineering (ALSACE)
  • Students in ENGR 660 in Fall 2003
Write a Comment
User Comments (0)
About PowerShow.com