890 likes | 901 Views
Teaching Inter-Object Design Patterns to Freshmen. Prasun Dewan UNC-Chapel Hill. Teaching Inter-Object Design Patterns to Freshmen. Recurring theme in programming Not captured by a programming construct Components Problem context Abstract solution Motivation for solution.
E N D
Teaching Inter-Object Design Patterns to Freshmen Prasun Dewan UNC-Chapel Hill
Teaching Inter-Object Design Patterns to Freshmen • Recurring theme in programming • Not captured by a programming construct • Components • Problem context • Abstract solution • Motivation for solution
TeachingInter-ObjectDesign 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)
TeachingInter-ObjectDesign 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)
Class Object Interface Inheritance TeachingInter-Object Design Patterns to Freshmen Iterator MVC Interactor Observer Facade Composite Factory
Iterator MVC Class Object Interactor Observer Interface Facade Inheritance Composite Factory TeachingInter-Object Design Patterns to Freshmen
Example Exercise TeachingInter-Object Design Patterns to Freshmen Class Object Interface Inheritance
TeachingInter-Object Design Patterns to Freshmen Class Object Interface Inheritance Example Exercise Monolithic, single- object programs
TeachingInter-Object Design Patterns to Freshmen Class Object Interface Inheritance Example Exercise Modular, multiple- object programs
TeachingInter-Object Design Patterns to Freshmen Class Object Interface Inheritance Example Exercise Implicit pattern
TeachingInter-Object Design Patterns to Freshmen Class Object Interface Inheritance Example Exercise Exact mimicking
TeachingInter-Object Design Patterns to Freshmen Class Object Interface Inheritance Example Exercise Matching vs. using
TeachingInter-Object Design Patterns to Freshmen Class Object Interface Inheritance Example Exercise Matching vs. using
TeachingInter-Object Design Patterns to Freshmen Class Object Interface Inheritance Example Exercise Matching vs. using
TeachingInter-Object Design Patterns to Freshmen Class Object Interface Inheritance Example Exercise Matching vs. using
TeachingInter-Object Design Patterns to Freshmen Class Object Interface Inheritance Example Exercise Explicit pattern
TeachingInter-Object Design Patterns to Freshmen Class Object Interface Inheritance Example Exercise
TeachingInter-Object Design Patterns to Freshmen Class Object Interface Inheritance Example Exercise
Compiler Toolkit Teaching Inter-Object Design Patterns toFreshmen Class Object Interface Inheritance Complex & Abstract Example Exercise Large
Teaching Inter-Object Design Patterns toFreshmen Diff. Approach Class Object Interface Inheritance Complex & Abstract Example Exercise Compiler Toolkit Large
Before After Teaching Inter-Object Design Patterns toFreshmen Diff. Approach Class Object Interface Inheritance Concrete Example Exercise Small (1 slide)
Before After ? ? Teaching Inter-Object Design Patterns toFreshmen Diff. Approach Class Object Interface Inheritance Concrete Example Exercise Medium (2 wk hw) Small (1 slide)
Before Teaching Inter-Object Design Patterns toFreshmen Before Class Object Interface After After Inheritance Concrete Example Exercise Counter Search Medium (2 wk hw)
Teaching Inter-Object Design Patterns toFreshmen Before Class Object Interface After Inheritance Concrete Example Exercise Counter Search
Counter • Can add arbitrary positive/negative value to an integer. • Different user interfaces.
Pattern-free Implementation publicclass ConsoleUI { staticint counter = 0; publicstaticvoid main(String[] args) { while (true) { int nextInput = readInt(); if (nextInput == 0) return; counter += nextInput; System.out.println("Counter: " + counter); } } }
Model Interactor Interactor MultipleUI ConsoleUI Counter Interactor MixedUI Model has no UI code and only semantics! Model/Interactor Separation
Composing Model and Interactor publicstatic main (String args[]) (new AConsoleUI()).interact (new ACounter()); }
Counter Model publicclass ACounter implements Counter { int counter = 0; publicvoid add (int amount) { counter += amount; } publicint getValue() { return counter; } } • Code reusability • Less duplication • Fewer changes
Shared model code Input Output Interactor publicclass AConsoleUI implements ConsoleUI { publicvoid interact (Counter counter) { while (true) { int nextInput = readInt(); if (nextInput == 0) return; counter.add(nextInput); System.out.println("Counter: " + counter.getValue()); } } }
MultipleUI ConsoleUI Counter MixedUI Duplicated input code Monolithic Inetractor Drawbacks MixedUI
MVC Pattern Controller View Performs Output Performs Input Read methods Model Write methods
MVC Pattern in Counter Controller View Performs Output Performs Input Model getValue() add()
Multiple Views and Controllers Controller 1 View 1 Controller 2 View 2 Model Controller 3 View 3 Controller M View N
Syncing Controllers & View Controller 1 View 1 Controller 2 View 2 Model Controller 3 View 3 Controller M View N
Observer Observable Observer/Observable Pattern Controller 1 View 1 Controller 2 View 2 Model Controller 3 View 3 Changed object notifies views Controller M View N
MVC Pattern Controller View Performs Output Performs Input Notification method Read methods Model Write methods
Observable Model publicclass AnObservableCounter extends ACounter implements ObservableCounter { Vector observers = new Vector(); publicvoid addObserver(CounterObserver observer) { observers.addElement(observer); observer.update(this); } publicvoid removeObserver(CounterObserver observer) { observers.removeElement(observer); } void notifyObservers() { for (int observerNum = 0; observerNum < observers.size(); observerNum++) ((CounterObserver) observers.elementAt(observerNum)).update(this); } publicvoid add (int amount) { super.add(amount); notifyObservers(); } }
Console View publicclass ACounterConsoleView implements CounterObserver { publicvoid update(ObservableCounter counter) { System.out.println("Counter: " + counter.getValue()); } }
Console Controller publicclass ACounterController implements CounterController { publicvoid processInput(Counter counter) { while (true) { int nextInput = readInt(); if (nextInput == 0) return; counter.add(nextInput); } } }
Input Code Shared Console Main publicstatic main (String args[]) Counter model = new ACounter(); model.addObserver (new AConsoleView()))); (new ACounterController()).processInput(model); }
Composition code duplicated Console Main publicstatic main (String args[]) Counter model = new ACounter(); model.addObserver (new AConsoleView()))); (new ACounterController()).processInput(model); }
Facade Façade Pattern Interactor Controller View Notification method Read methods Model Write methods
Console Interactor/Facade publicclass AConsoleInteractor implements ConsoleInteractor { publicvoid interact (ObservableCounter model) { model.addObserver(new ACounterConsoleView()); (new ACounterController()).processInput(model); }
Interactor-based Main publicstatic main (String args[]) (new AConsoleInteractor()).interact(new ACounter()); }
Assignments: Design-Patterns in the Medium Tokenizer Iterator Evaluator
Assignments: Design-Patterns in the Medium Tokenizer Facade Iterator Evaluator