0% found this document useful (0 votes)
16 views

Lecture 3

Strategy Pattern
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lecture 3

Strategy Pattern
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

Software Design Patterns

Introduction

1
Sequence
■ Motivation
■ Classification of Design Patterns
■ Benefits of Design Pattern
■ Design pattern Case Study – SimUDuck
■ Strategy Pattern

2
Motivation

3
Motivation

4
Two Main Catalogues

5
Motivation

6
Design Patterns Classification
GoF Design Patterns

Creational Structural Behavioral

class
scope

object
scope

7
Design Patterns Classification

8
Design Patterns Classification

9
Design Patterns Classification

10
11
What colleagues say to Rick…

12
The Power of Shared Pattern
Vocabulary
■ Shared pattern vocab are powerful
■ Pattern allows you to say more with less
■ Talking at pattern level allows you to stay “in the
design” longer
■ Shared vocab can turbo charge your development
team
■ Shared vocab encourage more junior developers to get
up to speed

13
14
Tools for Design Tool Box

15
Case Study: Design of SimUDuck App

16
Objectives of the Case Study
■ Go through a simple application design
■ See the problems of inflexible design as the new
requirements emerge
■ See how improvements are made based on some
fundamental design principles

17
Background…

18
Background…

19
It’s time for big innovation!

20
Requirement: We need Ducks to fly…

21
Joe’s New Design

22
But something went horribly wrong..
Rubber Ducks were also flying in demo…

23
24
Overridden Methods

25
Overridden Methods
■ Now, let's say you could actually create Animals.
◻ If you could, then calling makeSound() would call the method
defined in the superclass.
◻ If you create a specific Dog, calling makeSound() will display
"woof".
■ Now let's say you created a specific Cat.
◻ In that example, Cat does not have a makeSound() method, so
when you call makeSound() on Cat, it calls the method in the
superclass Animal and does nothing.
■ Remember, Cat and Dog are subclasses of Animal because they
extend Animal.

Read more: http://www.java-made-easy.com/polymorphism-in-java.html#ixzz2fvJ47iq4

26
Dynamic Method Binding
■ Dynamic method binding is how Java decides what method to call
when it has to decide between the superclass and the subclass.
■ So, how exactly does it decide?
■ We just made a Dog but declared it as an Animal!

■ In this example, what we are really creating is a Dog, even though


we are referencing the variable as an Animal.
■ Java decides from the object, which is created as which method to
call:

27
Dynamic Method Binding
■ Why in the world wouldn't we just do Dog dog = new Dog() ?
■ The answer is that by using abstraction, you can actually group
many different types of objects together.
■ Let's see how we can use this concept for storing objects:

28
29
New Requirement: Executives want the
product update 6 monthly…

30
How about Interface?

31
What do you think about this design?

It completely destroys the code reuse,


for fly and quake behaviors.
So it just creates a different maintenance
nightmare .
Looking for a design pattern???
32
1st Design Principle

33
Separating what changes from what
stays the same…

34
■ We want to assign behavior to instance of the Duck.
■ Even better, if can change the behavior dynamically

■ We will use interface to represent this behavior

35
36
Animal animal = getAnimal();

37
Implementing Duck Behavior

38
Design for Behavior Reuse

39
Integrating Duck Behavior

40
41
42
43
44
45
46
47
Setting Behavior Dynamically

48
Setting Behavior Dynamically

49
Setting Behavior Dynamically

50
51
The Big Picture on Encapsulated Behavior

Identify IS-A, HAS-A and IMPLEMENTS behaviors


52
3rd
Design
Principle

53
54
Assignment # 1
■ Develop and Execute this example in two languages
◻ Java
◻ C# or C++

■ Submit executable code in the form of project/solution, which can be


executed in Eclipse/Netbeans (Java) , Visual Studio (C# or C++)
■ Submission next week (online)

55
END Lec 3

56

You might also like