Title: Teaching Java Framework Design Using Classic Problems
1Teaching 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
2Outline
- Motivation
- Software families
- Foundational concepts
- Framework construction
- Divide and conquer case study
- Observations
- Framework generalization using function
generalization - Binary tree traversal case study
- Conclusions
3Technical 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
4Competitive 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
5One 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.
6How 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.
7Our 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
8Foundational Concepts
- Classic concepts (Parnas)
- information hiding modules
- abstract interfaces
- Object-oriented programming
- inheritance
- polymorphism
- Design patterns
9Information-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
10Abstract 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
11Design Patterns
- Well-established solutions to recurring design
problems - E.g. Template Method, Strategy, Composite,
Visitor, Singleton
12Software 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
Framework library
Frozen spots
Framework
Hot spots
User-supplied code(application specific)
13Hot Spot Subsystem
- Template method defines 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
14Framework Construction Principles
- Unification
- Use inheritance to implement hot spot subsystem
- Separation
- Use delegation to implement hot spot subsystem
15Unification Principle
16Separation Principle
17ExampleDivide 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?
18Well-Known Examples
- Mergesort
- Quicksort
- Heapify
- Binary search
- Fast matrix multiplication
- Fast Fourier Transform (FFT)
19Example Mergesort
1 12 10 6
1 12
10 6
12
10
6
1
6 10
1 12
1 6 10 12
20Divide and Conquer Pseudocode
- function solve(p)
- if isSimple(p)
- return simplySolve(p)
- else
-
- Problem sp decompose(p)
- for (i 0 iltsp.length i i1)
- soli solve(spi)
- return combine (sol)
-
21Divide Conquer Functions
- template method
- solve()
- hook methods
- isSimple()
- simplySolve()
- decompose()
- combine()
- other problem and solution descriptions
22Framework Template Method Pattern
23Template Method Class
- abstract public class DivConqTemplate
- final public Solution solve (Problem p)
- Problem pp
- 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)
24Framework 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
25Problem 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
26QuickSortDesc 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
27Quicksort 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
-
28Quicksort 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
-
29Quicksort 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 afirst
- afirst alast
- alast temp
-
-
30To 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
31To Use a Framework
- Implement concrete hook methods
- Plug subsystems into hot spots
32Educational 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
33Framework 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
34Binary 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)
35Function 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
36Composite Pattern for Structure
Component
BinTree
abstract getValue( )
abstract getLeft( )
abstract getright( )
abstract setValue( )
abstract setLeft( )
anstract setRight( )
Composite
Leaf
Node
Nil
1
37Binary Tree Component Base ClassPreorder
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
-
38Binary Tree Composite Node ClassPreorder
Traversal
- public class Node 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
39Binary Tree Leaf Node ClassPreorder 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()
-
40Framework 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?
41Frozen 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
42Hot Spots
- Variability in the visit operations 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
43Hot Spot 1Generalizing 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)
44Hot Spot 1Generalizing 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
- ...
45Hot Spot 2Generalizing 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)
46Hot Spot 2Generalizing 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) ...
47Hot Spot 3Generalizing the Tree Navigation
48Hot Spot 3Generalizing 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
49Hot Spot 3Euler 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
50Generalization 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
51Educational Observations
- Design of software families not easy
- Software framework design accessible to students
- Framework design builds on basic OO programming
concepts and technologies - Framework design best when carried out
systematically and incrementally - Teachers should begin with examples that are
already part of the students background
52Cunningham Group References
- H. C. Cunningham, Y. Liu, and C. Zhang. Using
Classic Problems to Teach Java Framework Design,
Science of Computer Programming, Special Issue on
Principles and Practice of Programming in Java
(PPPJ 2004), Vol. 59, No. 1-2, pp. 147-169,
January 2006. - H. C. Cunningham, Y. Liu, and P. Tadepalli.
Framework Design Using Function Generalization
A Binary Tree Traversal Case Study, In
Proceedings of the ACM SouthEast Conference, pp.
312-318, March 2006.
53Cunningham Group References
- H. C. Cunningham and P. Tadepalli. Using
Function Generalization to Design a Cosequential
Processing Framework, In Proceedings of the 39th
Hawaii International Conference on System
Sciences, 10 pages, IEEE, January 2006. - H. C. Cunningham, P. Tadepalli, and Y. Liu.
Secrets, Hot Spots, and Generalization
Preparing Students to Design Software Families,
Journal of Computing Sciences in Colleges, Vol.
20, No. 6, pp. 74-82, CCSC, June 2005.
54Other References
- D. L. Parnas. On the Design and Development of
Program Families, IEEE Transactions on Software
Engineering, Vol. SE-2, pp. 1-9, March 1976. - H. A. Schmid. Framework Design by Systematic
Generalization, In Building Application
Frameworks Object-Oriented Foundations of
Framework Design, M. E. Fayad, D. C. Schmidt, and
R. E. Johnson, Eds. Wiley, 1999, pp. 353378.
55Other References
- D. M. Weiss and C. T. R. Lai. Software
Product-Line Engineering A Family-Based
Software Development Process, Addison-Wesley,
1999.