Title: Teaching Inter-Object Design Patterns to Freshmen
1Teaching Inter-Object Design Patterns to Freshmen
- Prasun Dewan
- UNC-Chapel Hill
2Teaching Inter-Object Design Patterns to Freshmen
- Recurring theme in programming
- Not captured by a programming construct
- Components
- Problem context
- Abstract solution
- Motivation for solution
3Teaching Inter-Object Design Patterns to Freshmen
- Intra-Object
- Abstract algorithms in individual objects
- e.g. Loop patterns (Astrachan 98)
- Inter-Object
- Abstract ways in which multiple objects work
together - e.g. Observer, Façade (Gamma et al 95)
4Teaching Inter-Object Design Patterns to Freshmen
- Intra-Object
- Abstract algorithms in individual objects
- e.g. Loop patterns (Astrachan 98)
- Inter-Object
- Abstract ways in which multiple objects work
together - e.g. Observer, Façade (Gamma et al 95)
5Teaching Inter-Object Design Patterns to Freshmen
Iterator
MVC
Interactor
Observer
Facade
Composite
Factory
6Teaching Inter-Object Design Patterns to Freshmen
7Teaching Inter-Object Design Patterns to Freshmen
Class
Object
Interface
Inheritance
8Teaching Inter-Object Design Patterns to Freshmen
Class
Object
Interface
Inheritance
Example
Exercise
Monolithic, single- object programs
9Teaching Inter-Object Design Patterns to Freshmen
Class
Object
Interface
Inheritance
Example
Exercise
Modular, multiple- object programs
10Teaching Inter-Object Design Patterns to Freshmen
Class
Object
Interface
Inheritance
Example
Exercise
Implicit pattern
11Teaching Inter-Object Design Patterns to Freshmen
Class
Object
Interface
Inheritance
Example
Exercise
Exact mimicking
12Teaching Inter-Object Design Patterns to Freshmen
Class
Object
Interface
Inheritance
Example
Exercise
Matching vs. using
13Teaching Inter-Object Design Patterns to Freshmen
Class
Object
Interface
Inheritance
Example
Exercise
Matching vs. using
14Teaching Inter-Object Design Patterns to Freshmen
Class
Object
Interface
Inheritance
Example
Exercise
Matching vs. using
15Teaching Inter-Object Design Patterns to Freshmen
Class
Object
Interface
Inheritance
Exercise
Example
Matching vs. using
16Teaching Inter-Object Design Patterns to Freshmen
Class
Object
Interface
Inheritance
Example
Exercise
Explicit pattern
17Teaching Inter-Object Design Patterns to Freshmen
Class
Object
Interface
Inheritance
Example
Exercise
18Teaching Inter-Object Design Patterns to Freshmen
Class
Object
Interface
Inheritance
Example
Exercise
19Teaching Inter-Object Design Patterns to Freshmen
Class
Object
Interface
Inheritance
Example
Exercise
Complex Abstract
Large
20Teaching Inter-Object Design Patterns to Freshmen
Diff. Approach
Class
Object
Interface
Inheritance
Example
Exercise
Complex Abstract
Compiler
Toolkit
Large
21Teaching Inter-Object Design Patterns to Freshmen
Diff. Approach
Class
Object
Interface
Inheritance
Example
Exercise
Concrete
Small (1 slide)
22Teaching Inter-Object Design Patterns to Freshmen
Diff. Approach
Class
Object
Interface
Inheritance
Example
Exercise
Concrete
Medium (2 wk hw)
Small (1 slide)
23Teaching Inter-Object Design Patterns to Freshmen
Before
Class
Object
Interface
After
After
Inheritance
Example
Exercise
Concrete
Counter
Search
Medium (2 wk hw)
24Teaching Inter-Object Design Patterns to Freshmen
Before
Class
Object
Interface
After
Inheritance
Example
Exercise
Concrete
Counter
Search
25Counter
- Can add arbitrary positive/negative value to an
integer. - Different user interfaces.
26Console Input and Output
27Console Input and JOption Output
28Console Input,Output and JOption Output
29Pattern-free Implementation
public class ConsoleUI static int
counter 0 public static void
main(String args) while (true) int
nextInput readInt() if (nextInput 0)
return counter nextInput
System.out.println("Counter " counter)
30Model/Interactor Separation
31Composing Model and Interactor
public static main (String args) (new
AConsoleUI()).interact (new ACounter())
32Counter Model
public class ACounter implements Counter int
counter 0 public void add (int amount)
counter amount public int getValue()
return counter
- Code reusability
- Less duplication
- Fewer changes
33Interactor
public class AConsoleUI implements ConsoleUI
public void interact (Counter
counter) while (true) int nextInput
readInt() if (nextInput 0) return
counter.add(nextInput) System.out.println(
"Counter " counter.getValue())
34Monolithic Inetractor Drawbacks
MixedUI
35MVC Pattern
Controller
View
Performs Output
Performs Input
Read methods
Model
Write methods
36MVC Pattern in Counter
Controller
View
Performs Output
Performs Input
Model
getValue()
add()
37Multiple Views and Controllers
Controller 1
View 1
Controller 2
View 2
Model
Controller 3
View 3
Controller M
View N
38Syncing Controllers View
Controller 1
View 1
Controller 2
View 2
Model
Controller 3
View 3
Controller M
View N
39Observer/Observable Pattern
Controller 1
View 1
Controller 2
View 2
Model
Controller 3
View 3
Changed object notifies views
Controller M
View N
40MVC Pattern
Controller
View
Performs Output
Performs Input
Notification method
Read methods
Model
Write methods
41Observable Model
public class AnObservableCounter extends ACounter
implements ObservableCounter Vector observers
new Vector() public void addObserver(CounterOb
server observer) observers.addElement(observer
) observer.update(this) public void
removeObserver(CounterObserver observer)
observers.removeElement(observer) void
notifyObservers() for (int observerNum 0
observerNum lt observers.size()
observerNum) ((CounterObserver)
observers.elementAt(observerNum)).update(this)
public void add (int amount) super.add(amoun
t) notifyObservers()
42Console View
public class ACounterConsoleView implements
CounterObserver public void update(ObservableCo
unter counter) System.out.println("Counter "
counter.getValue())
43Console Controller
public class ACounterController implements
CounterController public void
processInput(Counter counter) while (true)
int nextInput readInt() if (nextInput
0) return counter.add(nextInput)
44Console Main
public static main (String args) Counter
model new ACounter() model.addObserver
(new AConsoleView()))) (new
ACounterController()).processInput(model)
45Console Main
public static main (String args) Counter
model new ACounter() model.addObserver
(new AConsoleView()))) (new
ACounterController()).processInput(model)
46Façade Pattern
Interactor
Controller
View
Notification method
Read methods
Model
Write methods
47Console Interactor/Facade
public class AConsoleInteractor implements
ConsoleInteractor public void interact
(ObservableCounter model) model.addObserver(ne
w ACounterConsoleView()) (new
ACounterController()).processInput(model)
48Interactor-based Main
public static main (String args) (new
AConsoleInteractor()).interact(new ACounter())
49Assignments Design-Patterns in the Medium
Tokenizer
Iterator
Evaluator
50Assignments Design-Patterns in the Medium
Tokenizer
Facade
Iterator
Evaluator
51Assignments Design-Patterns in the Medium
Tokenizer
Facade
Iterator
Evaluator
52Assignments Design-Patterns in the Medium
Interactor
Spreadesheet Model
53Classroom Experience
- 2003
- 37 students
- 10 freshmen
- 1 high school junior
- Pre-requisite conventional programming
- Primitive types
- Arrays
- Loops
- Procedures
- O-O programming
- Classes
- Interfaces
- Inheritance
- Design patterns
- MVC, observer, façade, interactor
- iterator, factory, composite
- visitor, adapter, proxy
54Evaluation
- Overall class performance very good
- 33 out of 37 students finished spreadsheet
- Freshmen were more enthusiastic and had better
grades - High school junior had highest score in first
midterm - Enthusiasm and intellect more important than
background and college experience
55Evaluation Survey of 27 students
Pattern Not well understood Memorable Forgettable
Iterator 0 5 1
MVC 6 8 3
Observer 5 7 4
Composite 0 9 0
Factory 3 7 3
Facade 2 9 2
56Conclusions
- To fully understand patterns need before and
after pattern use comparison with exact code
changes shown. - It is possible to present in classroom MVC,
interactor, façade, iterator, composite, and
factory design patterns in the small - Each interface/class fits in a slide
- This experience can be used to exercise design
patterns in the medium - A single project incrementally created in 2-week
assignments. - Spreadsheet project had about 40
classes/interfaces - More patterns and experience needed.
- More details http//www.cs.unc.edu/dewan/comp114
/
57The End
58Recursive Composition
AnObservable Counter
CounterJOptionView
Console View
ConsoleController
59Observer/Observable Pattern
Notification Method
Observer 1
Observer 2
Observable 1
Observer 3
Observer N
60Console Controller And View Main
package main import models.AnObservableCounter i
mport facades.AConsoleControllerAndView public
class ACounterMain public static void
main(String args) (new
AConsoleControllerAndView()).edit(new
AnObservableCounter())
61Console Controller And JOption View Main
package main import models.AnObservableCounter i
mport facades.AConsoleControllerAndJOptionView pu
blic class ACounterMain public static void
main(String args) (new
AConsoleContollerAndJOptionView()).edit(new
AnObservableCounter())
62ConsoleControllerAndJView Facade
package facades import models.ObservableCounter
import models.CounterObserver import
controllers.ACounterController import
controllers.CounterController import
views.ACounterJOptionView public class
AConsoleControllerAndJOptionView implements
CounterInteractor public void
edit(ObservableCounter model) CounterObserver
view new ACounterJOptionView() model.addObser
ver(view) CounterController controller new
ACounterController() controller.setModel(model)
controller.processInput()
63Model/Interactor Pattern
Interactor
UI Code
Arbitrary UI unaware methods
Model
Computation code
64Main with two views
package main import models.AnObservableCounter i
mport facades.AConsoleControllerAndViewAndJOptionV
iew public class ACounterMain public static
void main(String args) (new
AConsoleControllerAndViewAndJOptionView()).edit(ne
w AnObservableCounter())
65Facade over facade
package facades import models.ObservableCounter
import models.CounterObserver import
views.ACounterJOptionView public class
AConsoleContollerAndViewAndJOptionView implements
CounterInteractor public void
edit(ObservableCounter model) model.addObserve
r(new ACounterJOptionView()) (new
AConsoleContollerAndView()).edit(model)
66Main with two views and OE
package main import models.AnObservableCounter i
mport bus.uigen.ObjectEditor import
facades.AConsoleControllerAndViewAndJOptionView p
ublic class ACounterMain public static void
main(String args)
ObservableCounter model new AnObservableCounter
() (new ObjectEditor()).edit(model) (new
ConsoleControllerAndViewAndJOptionView()).edit(mod
el)
67Observers that are not views
- Spreadsheet cell observes cells on which it
depends.. - Monitoring of appliance usage
- Each time I do setChannel() on TV event logged.
- Any big brother app!
- Counter observer?
68Rocket Observer
69Instances created and composed
AnObservable Counter
ACounterConsole View
ACounterController
ARocket
AConsoleControllerAndView
ARocketLauncher
70Rocket Interface
package models import models.CounterObserver pub
lic interface Rocket extends CounterObserver
public void launch()
71Rocket Launching Facade
package models import models.ObservableCounter p
ublic class ARocket implements Rocket public
void update(ObservableCounter counter) if
(counter.getValue() 0) launch() public
void launch() System.out.println("LIFT
OFF!!!")
72Rocket Launching Facade
package facades import models.ObservableCounter
import models.CounterObserver import
models.ARocket import facades.AConsoleContollerAn
dView public class ARocketLaunchCountDown
implements CounterInteractor public final int
INITIAL_COUNTER_VALUE 10 public void
edit(ObservableCounter counter)
counter.add(INITIAL_COUNTER_VALUE) Counter
Observer rocket new ARocket() counter.addObse
rver(rocket) (new AConsoleContollerAndView()).e
dit(counter)
73Rocket launching main
package main import models.AnObservableCounter i
mport models.ARocketLauncher import
facades.ARocketLaunchCountDown public class
ACounterMain public static void main(String
args) (new ARocketLaunchCountDown()).
edit(new AnObservableCounter())
74(No Transcript)
75Teaching Inter-Object Design Patterns to Freshmen
?
Interface
Inheritance
Example
Exercise
?
?
?
?
Small
Medium
76Teaching Inter-Object Design Patterns to Freshmen
Iterator
MVC
Class
Object
Interactor
Observer
Interface
Facade
Inheritance
Composite
Factory
4 vs. ?? pages
77Teaching Inter-Object Design Patterns to Freshmen
Iterator
MVC
Class
Object
Interactor
Observer
Interface
Facade
Inheritance
Composite
Factory
4 vs. ?? pages
78Teaching Inter-Object Design Patterns to Freshmen
Iterator
MVC
Class
Object
Interactor
Observer
Interface
Facade
Inheritance
Composite
Factory
79Teaching Inter-Object Design Patterns to Freshmen
Iterator
MVC
Class
Object
Interactor
Observer
Interface
Facade
Inheritance
Composite
Factory
80Teaching Inter-Object Design Patterns to Freshmen
Class
Object
Interface
Inheritance
Example
Exercise
Modular
81Teaching Inter-Object Design Patterns to Freshmen
Class
Object
Interface
Inheritance
Example
Exercise
82Teaching Inter-Object Design Patterns to Freshmen
Iterator
MVC
Class
Object
Facade
Interactor
Interface
Factory
Composite
Inheritance
83Teaching Design Patterns to Freshmen
- Prasun Dewan
- UNC-Chapel Hill
84Teaching Design Patterns to Freshmen
85Teaching Inter-Object Design Patterns to Freshmen
- Recurring theme in programming
- Not captured by a programming construct
- Design pattern framework architecture
- Components
- Problem context
- Abstract solution
- Pros/cons of solution
86Motivates Model/UI Separation
87Counter Model
public class ACounter implements Counter int
counter 0 public void add (int amount)
counter amount public int getValue()
return counter
- Code reusability
- Less duplication
- Fewer changes
88Interactor/Model Pattern
Interactor
Performs Output
Performs Input
Notification method
Read methods
Model
Write methods
aka Model/View Pattern
89Console UI
public class ConsoleUI static Counter
counter new ACounter() public static
void main(String args) while (true)
int nextInput readInt() if (nextInput
0) return counter.add(nextInput)
System.out.println("Counter "
counter.getValue())