1 / 89

Teaching Inter-Object Design Patterns to Freshmen

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.

davidavery
Download Presentation

Teaching Inter-Object Design Patterns to Freshmen

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Teaching Inter-Object Design Patterns to Freshmen Prasun Dewan UNC-Chapel Hill

  2. 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

  3. 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)

  4. 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)

  5. Class Object Interface Inheritance TeachingInter-Object Design Patterns to Freshmen Iterator MVC Interactor Observer Facade Composite Factory

  6. Iterator MVC Class Object Interactor Observer Interface Facade Inheritance Composite Factory TeachingInter-Object Design Patterns to Freshmen

  7. Example Exercise TeachingInter-Object Design Patterns to Freshmen Class Object Interface Inheritance

  8. TeachingInter-Object Design Patterns to Freshmen Class Object Interface Inheritance Example Exercise Monolithic, single- object programs

  9. TeachingInter-Object Design Patterns to Freshmen Class Object Interface Inheritance Example Exercise Modular, multiple- object programs

  10. TeachingInter-Object Design Patterns to Freshmen Class Object Interface Inheritance Example Exercise Implicit pattern

  11. TeachingInter-Object Design Patterns to Freshmen Class Object Interface Inheritance Example Exercise Exact mimicking

  12. TeachingInter-Object Design Patterns to Freshmen Class Object Interface Inheritance Example Exercise Matching vs. using

  13. TeachingInter-Object Design Patterns to Freshmen Class Object Interface Inheritance Example Exercise Matching vs. using

  14. TeachingInter-Object Design Patterns to Freshmen Class Object Interface Inheritance Example Exercise Matching vs. using

  15. TeachingInter-Object Design Patterns to Freshmen Class Object Interface Inheritance Example Exercise Matching vs. using

  16. TeachingInter-Object Design Patterns to Freshmen Class Object Interface Inheritance Example Exercise Explicit pattern

  17. TeachingInter-Object Design Patterns to Freshmen Class Object Interface Inheritance Example Exercise

  18. TeachingInter-Object Design Patterns to Freshmen Class Object Interface Inheritance Example Exercise

  19. Compiler Toolkit Teaching Inter-Object Design Patterns toFreshmen Class Object Interface Inheritance Complex & Abstract Example Exercise Large

  20. Teaching Inter-Object Design Patterns toFreshmen Diff. Approach Class Object Interface Inheritance Complex & Abstract Example Exercise Compiler Toolkit Large

  21. Before After Teaching Inter-Object Design Patterns toFreshmen Diff. Approach Class Object Interface Inheritance Concrete Example Exercise Small (1 slide)

  22. Before After ? ? Teaching Inter-Object Design Patterns toFreshmen Diff. Approach Class Object Interface Inheritance Concrete Example Exercise Medium (2 wk hw) Small (1 slide)

  23. Before Teaching Inter-Object Design Patterns toFreshmen Before Class Object Interface After After Inheritance Concrete Example Exercise Counter Search Medium (2 wk hw)

  24. Teaching Inter-Object Design Patterns toFreshmen Before Class Object Interface After Inheritance Concrete Example Exercise Counter Search

  25. Counter • Can add arbitrary positive/negative value to an integer. • Different user interfaces.

  26. Console Input and Output

  27. Console Input and JOption Output

  28. Console Input,Output and JOption Output

  29. 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); } } }

  30. Model Interactor Interactor MultipleUI ConsoleUI Counter Interactor MixedUI Model has no UI code and only semantics! Model/Interactor Separation

  31. Composing Model and Interactor publicstatic main (String args[]) (new AConsoleUI()).interact (new ACounter()); }

  32. 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

  33. 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()); } } }

  34. MultipleUI ConsoleUI Counter MixedUI Duplicated input code Monolithic Inetractor Drawbacks MixedUI

  35. MVC Pattern Controller View Performs Output Performs Input Read methods Model Write methods

  36. MVC Pattern in Counter Controller View Performs Output Performs Input Model getValue() add()

  37. Multiple Views and Controllers Controller 1 View 1 Controller 2 View 2 Model Controller 3 View 3 Controller M View N

  38. Syncing Controllers & View Controller 1 View 1 Controller 2 View 2 Model Controller 3 View 3 Controller M View N

  39. 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

  40. MVC Pattern Controller View Performs Output Performs Input Notification method Read methods Model Write methods

  41. 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(); } }

  42. Console View publicclass ACounterConsoleView implements CounterObserver { publicvoid update(ObservableCounter counter) { System.out.println("Counter: " + counter.getValue()); } }

  43. Console Controller publicclass ACounterController implements CounterController { publicvoid processInput(Counter counter) { while (true) { int nextInput = readInt(); if (nextInput == 0) return; counter.add(nextInput); } } }

  44. Input Code Shared Console Main publicstatic main (String args[]) Counter model = new ACounter(); model.addObserver (new AConsoleView()))); (new ACounterController()).processInput(model); }

  45. Composition code duplicated Console Main publicstatic main (String args[]) Counter model = new ACounter(); model.addObserver (new AConsoleView()))); (new ACounterController()).processInput(model); }

  46. Facade Façade Pattern Interactor Controller View Notification method Read methods Model Write methods

  47. Console Interactor/Facade publicclass AConsoleInteractor implements ConsoleInteractor { publicvoid interact (ObservableCounter model) { model.addObserver(new ACounterConsoleView()); (new ACounterController()).processInput(model); }

  48. Interactor-based Main publicstatic main (String args[]) (new AConsoleInteractor()).interact(new ACounter()); }

  49. Assignments: Design-Patterns in the Medium Tokenizer Iterator Evaluator

  50. Assignments: Design-Patterns in the Medium Tokenizer Facade Iterator Evaluator

More Related