0% found this document useful (0 votes)
63 views1 page

4-Design Patterns - Google Slides

The document discusses different designs for modeling duck behavior in a simulation. Design 1 allowed for different duck types but was not extensible. Design 2 addressed this flaw by overriding methods in subclasses but created maintenance issues. Design 3 proposes using interfaces to allow changing behaviors in a more extensible way.

Uploaded by

Sita kumar
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)
63 views1 page

4-Design Patterns - Google Slides

The document discusses different designs for modeling duck behavior in a simulation. Design 1 allowed for different duck types but was not extensible. Design 2 addressed this flaw by overriding methods in subclasses but created maintenance issues. Design 3 proposes using interfaces to allow changing behaviors in a more extensible way.

Uploaded by

Sita kumar
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/ 1

4-Design Patterns Slideshow Share

File Edit View Insert Tools Help

Fit Comment only

1
Design Patterns

2 Design Patterns
Someone has already solved your problems !!

3 Duck Simulator Example (source: Head First Design Patterns, 2nd ed.)
● This simulator is for showing large variety of ducks quacking, flying
etc.
● This is not for one type of duck. Many different variety of ducks
○ eg. Mallard duck, Redhead duck

Design Patterns
4 Class Diagram: Design 1:

5 Design 1: Extensibility

6 Design 1: Flaw

Can’t fly Can Quack

Rubber duck

7 Design 2: Solution to Design 1 Flaw


Duck

quack()
swim()
display()
fly()
RubberDuck

display() {
//looks like a redhead
MallardDuck }
RedHeadDuck fly() {
//override to do
display() { nothing
//looks like a mallard display() { }
} //looks like a redhead quack() {
} //overridden to
squeak
}

8 Design 2: Flaw
● Overriding solution is not a cleaner way
● There will be issues with maintenance when there are
modifications in the future.
● Whenever a new Duck subclass is added all the required overriding
have to be done.

9 Design 3: Interfaces of Changing Behaviours

You might also like