and professional practice l8.5: object-oriented design...

18
Design Patterns 1 Software Development and Professional Practice L8.5: Object-Oriented Design - Have I got a Strategy!

Upload: others

Post on 20-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: and Professional Practice L8.5: Object-Oriented Design ...faculty.knox.edu/jdooley/SDWebPage/SlidesInPDF/L8.2Strategy_Patt… · Design Patterns 1 Software Development and Professional

Design Patterns 1

Software Developmentand Professional Practice

L8.5: Object-Oriented Design - Have I got a Strategy!

Page 2: and Professional Practice L8.5: Object-Oriented Design ...faculty.knox.edu/jdooley/SDWebPage/SlidesInPDF/L8.2Strategy_Patt… · Design Patterns 1 Software Development and Professional

Intro to Development 2

Unless otherwise expressly stated, all original material of whatever nature created by John F. Dooley and included in this web site and any related pages, including the site's archives, is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

Page 3: and Professional Practice L8.5: Object-Oriented Design ...faculty.knox.edu/jdooley/SDWebPage/SlidesInPDF/L8.2Strategy_Patt… · Design Patterns 1 Software Development and Professional

Design Patterns 3

SimUDuck!

Page 4: and Professional Practice L8.5: Object-Oriented Design ...faculty.knox.edu/jdooley/SDWebPage/SlidesInPDF/L8.2Strategy_Patt… · Design Patterns 1 Software Development and Professional

Design Patterns 4

Page 5: and Professional Practice L8.5: Object-Oriented Design ...faculty.knox.edu/jdooley/SDWebPage/SlidesInPDF/L8.2Strategy_Patt… · Design Patterns 1 Software Development and Professional

Design Patterns

Now we want to fly!

So how to add flying to SimUDuck?

5

Page 6: and Professional Practice L8.5: Object-Oriented Design ...faculty.knox.edu/jdooley/SDWebPage/SlidesInPDF/L8.2Strategy_Patt… · Design Patterns 1 Software Development and Professional

Design Patterns

Is this OK? Looks OK, right? So now we can add more ducks, right?

6

What don’t these ducks do?

Page 7: and Professional Practice L8.5: Object-Oriented Design ...faculty.knox.edu/jdooley/SDWebPage/SlidesInPDF/L8.2Strategy_Patt… · Design Patterns 1 Software Development and Professional

Design Patterns

So what to do?

Anything wrong with this?

So, inheritance might not be the right thing to use here Sound familiar?

7

Page 8: and Professional Practice L8.5: Object-Oriented Design ...faculty.knox.edu/jdooley/SDWebPage/SlidesInPDF/L8.2Strategy_Patt… · Design Patterns 1 Software Development and Professional

Design Patterns

Back to OO Design principles...

take the parts of your system that vary and encapsulate them, so that later you can alter or extend the parts that vary without affecting those that don’t.

8

Page 9: and Professional Practice L8.5: Object-Oriented Design ...faculty.knox.edu/jdooley/SDWebPage/SlidesInPDF/L8.2Strategy_Patt… · Design Patterns 1 Software Development and Professional

Design Patterns

So what changes?

9

Page 10: and Professional Practice L8.5: Object-Oriented Design ...faculty.knox.edu/jdooley/SDWebPage/SlidesInPDF/L8.2Strategy_Patt… · Design Patterns 1 Software Development and Professional

Design Patterns

So what changes?

10

Page 11: and Professional Practice L8.5: Object-Oriented Design ...faculty.knox.edu/jdooley/SDWebPage/SlidesInPDF/L8.2Strategy_Patt… · Design Patterns 1 Software Development and Professional

Design Patterns 11

Page 12: and Professional Practice L8.5: Object-Oriented Design ...faculty.knox.edu/jdooley/SDWebPage/SlidesInPDF/L8.2Strategy_Patt… · Design Patterns 1 Software Development and Professional

Design Patterns

So for the Ducks...

12

Page 13: and Professional Practice L8.5: Object-Oriented Design ...faculty.knox.edu/jdooley/SDWebPage/SlidesInPDF/L8.2Strategy_Patt… · Design Patterns 1 Software Development and Professional

Design Patterns 13

Page 14: and Professional Practice L8.5: Object-Oriented Design ...faculty.knox.edu/jdooley/SDWebPage/SlidesInPDF/L8.2Strategy_Patt… · Design Patterns 1 Software Development and Professional

Design Patterns 14

Page 15: and Professional Practice L8.5: Object-Oriented Design ...faculty.knox.edu/jdooley/SDWebPage/SlidesInPDF/L8.2Strategy_Patt… · Design Patterns 1 Software Development and Professional

Design Patterns

Final version...

15

Page 16: and Professional Practice L8.5: Object-Oriented Design ...faculty.knox.edu/jdooley/SDWebPage/SlidesInPDF/L8.2Strategy_Patt… · Design Patterns 1 Software Development and Professional

Design Patterns

And so what have we learned?

16

Page 17: and Professional Practice L8.5: Object-Oriented Design ...faculty.knox.edu/jdooley/SDWebPage/SlidesInPDF/L8.2Strategy_Patt… · Design Patterns 1 Software Development and Professional

Design Patterns

Applicability of Strategy

Use the Strategy pattern when many related classes differ only in their behavior. you need different variants of an algorithm. an algorithm uses data that clients shouldn’t know

about. Use the Strategy pattern to avoid exposing complex, algorithm-specific data structures.

a class defines many behaviors, and these appear as multiple conditional statements in its operations. Instead of many conditionals, move related conditional branches into their own Strategy class.

(all this directly from GoF, page 316.)

17

Page 18: and Professional Practice L8.5: Object-Oriented Design ...faculty.knox.edu/jdooley/SDWebPage/SlidesInPDF/L8.2Strategy_Patt… · Design Patterns 1 Software Development and Professional

Design Patterns 18

References Freeman, Eric and Freeman, Elisabeth, Head First Design

Patterns, O’Reilly, 2004. Dooley, John F., Software Development and Professional

Practice, Apress, 2011.