16-introdesignpatterns

16-introdesignpatterns - Introduction to Design Patterns...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Design Patterns Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 — Lecture 16 — 10/15/2009 © University of Colorado, 2009 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture Goals • Cover Material from Chapter 1 and 2 of the Design Patterns Textbook • Introduction to Design Patterns • Strategy Pattern • Observer Pattern 2
Background image of page 2
Why Patterns? (I) • As the Design Guru says • “Remember, knowing concepts like abstraction , inheritance , and polymorphism • do not make you a good OO designer. • A design guru thinks about how to create fexible designs that are maintainable and that can cope with change .” 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Why Patterns? (II) • Someone has already solved your problems (!) • Design patterns allow you to exploit the wisdom and lessons learned by other developers who’ve encountered design problems similar to the ones you are encountering • The best way to use design patterns is to load your brain with them and then recognize places in your designs and existing applications where you can apply them • Instead of code reuse , you get experience reuse 4
Background image of page 4
Design Pattern by Example • SimUDuck: a “duck pond simulator” that can show a wide variety of duck species swimming and quacking • Initial State • But a request has arrived to allow ducks to also Fy. (We need to stay ahead of the competition!) quack() swim() display() Duck display() MallardDuck display() RedheadDuck 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Easy quack() swim() display() Duck display() MallardDuck display() RedheadDuck quack() swim() display() fy() Duck display() MallardDuck display() RedheadDuck Code Reuse via Inheritance Add fy() to Duck; all ducks can now fy 6
Background image of page 6
Whoops! quack() swim() display() fy() Duck display() MallardDuck display() RedheadDuck quack() display() RubberDuck Rubber ducks do not fy! They don’t quack either, so we had previously overridden quack() to make them squeak. We could override fy() in RubberDuck to make it do nothing, but that’s less than ideal, especially. .. 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Double Whoops! quack() swim() display() fy() Duck display() MallardDuck display() RedheadDuck display() quack() fy() RubberDuck display() quack() fy() DecoyDuck …when we might always fnd other Duck subclasses that would have to do the same thing. What was supposed to be a good instance oF reuse via inheritance has turned into a maintenance headache ! 8
Background image of page 8
What about an Interface? swim() display() Duck display() fy() quack() MallardDuck display() fy() quack() RedheadDuck display() quack() RubberDuck display() DecoyDuck fy() Flyable «Interface» quack() Quackable «Interface» Here we deFne two interfaces and allow subclasses to implement the interfaces they need. What are the trade-offs? 9
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Design Trade-Offs • With inheritance, we get • code reuse, only one fy() and quack() method vs. multiple (pro) • common behavior in root class, not so common aFter all (con) • With interFaces, we get • speci±city: only those subclasses that need a fy() method get it (pro) • no code re-use: since interFaces only de±ne signatures (con)
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/11/2010 for the course CSCI 5448 taught by Professor Anderson during the Fall '09 term at Colorado.

Page1 / 36

16-introdesignpatterns - Introduction to Design Patterns...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online