0% found this document useful (0 votes)
46 views25 pages

OODP Strategy

This document discusses object-oriented design patterns for modeling duck behavior in a simulation game. It describes using inheritance to define different duck types which results in code duplication. Using interfaces to define behaviors like flying and quacking avoids this by allowing different objects to share behaviors. The strategy pattern is presented as a way to set behaviors at runtime through composition rather than inheritance. This allows modifying behaviors without changing classes.

Uploaded by

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

OODP Strategy

This document discusses object-oriented design patterns for modeling duck behavior in a simulation game. It describes using inheritance to define different duck types which results in code duplication. Using interfaces to define behaviors like flying and quacking avoids this by allowing different objects to share behaviors. The strategy pattern is presented as a way to set behaviors at runtime through composition rather than inheritance. This allows modifying behaviors without changing classes.

Uploaded by

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

OO Design Patterns

Object-Oriented Programming
Simple SimUDuck app

A duck pond simulation game


 A large variety of duck species

swimming and making quacking sounds


 Examples of duck species:

 mallards, red head ducks, mandarin ducks

Đại học Công nghệ - ĐHQG HN OO Design Pattern 2


First Solution

Đại học Công nghệ - ĐHQG HN OO Design Pattern 3


Then… ducks need to fly
 How to change the design?

Đại học Công nghệ - ĐHQG HN OO Design Pattern 4


What about rubber duckies?

Đại học Công nghệ - ĐHQG HN OO Design Pattern 5


What about decoy ducks?

Đại học Công nghệ - ĐHQG HN OO Design Pattern 6


Disadvantage of using inheritance to
provide Duck behavior?

A. Code is duplicated across subclasses.


B. Runtime behavior changes are difficult
C. We can’t make duck dances.
D. Hard to gain knowledge of all duck behaviors
E. Ducks can’t fly and quack at the same time
F. Changes can unintentionally affect other ducks

What do you think?

Đại học Công nghệ - ĐHQG HN OO Design Pattern 7


Your solution?

Đại học Công nghệ - ĐHQG HN OO Design Pattern 8


Using interfaces?
 What do you think about this design?

Đại học Công nghệ - ĐHQG HN OO Design Pattern 9


Design Principle #1

Separate things that vary

from things that stay the same

Đại học Công nghệ - ĐHQG HN OO Design Pattern 10


Separating what varies
from what stays the same

Đại học Công nghệ - ĐHQG HN OO Design Pattern 11


Design Principle #2

Program to an interface

not an implementation

Đại học Công nghệ - ĐHQG HN OO Design Pattern 12


Đại học Công nghệ - ĐHQG HN OO Design Pattern 13
Examples

Đại học Công nghệ - ĐHQG HN OO Design Pattern 14


Examples
 Programming to an implementation

 Programming to an interface/superclass
can assign the concrete implementation at run-time

Đại học Công nghệ - ĐHQG HN OO Design Pattern 15


Behavior interfaces and classes

 Other types of objects can reuse fly and quack


behaviors
 We can add new behaviors w/o modifying any of the
existing behavior classes or touching any of the
Duck classes that use flying behavior

Đại học Công nghệ - ĐHQG HN OO Design Pattern 16


Behavior interfaces and classes

 Other types of objects can reuse fly and quack


behaviors
 We can add new behaviors w/o modifying any of the
existing behavior classes or touching any of the
Duck classes that use flying behavior

Đại học Công nghệ - ĐHQG HN OO Design Pattern 17


Behavior interfaces and classes

 Other types of objects can reuse fly and quack


behaviors
 We can add new behaviors w/o modifying any of the
existing behavior classes or touching any of the
Duck classes that use flying behavior

Đại học Công nghệ - ĐHQG HN OO Design Pattern 18


Superclass Duck

Đại học Công nghệ - ĐHQG HN OO Design Pattern 19


Superclass Duck

Đại học Công nghệ - ĐHQG HN OO Design Pattern 20


Duck’s Subclasses

Đại học Công nghệ - ĐHQG HN OO Design Pattern 21


Setting Behavior dynamically
 How?

Đại học Công nghệ - ĐHQG HN OO Design Pattern 22


Design Principle #3

Favor composition

over inheritance

Đại học Công nghệ - ĐHQG HN OO Design Pattern 23


The pattern

Đại học Công nghệ - ĐHQG HN OO Design Pattern 24


Homework

Đại học Công nghệ - ĐHQG HN OO Design Pattern 25

You might also like