Chapter 1: Introduction to Design Patterns. SimUDuck Example.

20
Chapter 1: Introduction to Design Patterns

Transcript of Chapter 1: Introduction to Design Patterns. SimUDuck Example.

Page 1: Chapter 1: Introduction to Design Patterns. SimUDuck Example.

Chapter 1: Introduction to Design Patterns

Page 2: Chapter 1: Introduction to Design Patterns. SimUDuck Example.

SimUDuck Example

quack()swim()display()

Duck

display()

Mallard

display()

RedheadDuck other ducks...

Page 3: Chapter 1: Introduction to Design Patterns. SimUDuck Example.

System Update

• Revise the current system to cater for ducks that fly. How can this be done?

Page 4: Chapter 1: Introduction to Design Patterns. SimUDuck Example.

A Possible Solution

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

Duck

display()

MallardDuck

display()

RedheadDuckother ducks...

Suppose that a RubberDuckclass needs to be added?

Page 5: Chapter 1: Introduction to Design Patterns. SimUDuck Example.

Revision: Use Inheritance?

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

Duck

display()

MallardDuck

display()

RedheadDuck

display()

RubberDuck

quack()fly()

Suppose that you have to add a decoy duck that does not fly or quack…

Page 6: Chapter 1: Introduction to Design Patterns. SimUDuck Example.

Another Solution: Interfaces

• The aspects that change for each type of duck are the methods fly() and quack().

• Take these methods out of the Duck class.• Create interfaces for these methods:

– Flyable – has a method fly()– Quackable – has a method quack()

Page 7: Chapter 1: Introduction to Design Patterns. SimUDuck Example.

Using Interfaces

swim()display()

Duck

display()fly()quack()

MallardDuck

display()fly()quack()

RedheadDuck

display()

RubberDuck

quack()

fly()

Flyable

quack()

Quackable

display()

DecoyDuck

Problems with this?

Page 8: Chapter 1: Introduction to Design Patterns. SimUDuck Example.

Disadvantages of the Approach

• All methods in Java interfaces are abstract.• Code has to be duplicated for classes.• Modification will have to be made to more

than one class.• This will introduce bugs.

Page 9: Chapter 1: Introduction to Design Patterns. SimUDuck Example.

Design Principle

• Identify the aspects of the application that vary and separate them from what stays the same.

• Encapsulate the parts that vary.• Future changes can be made without affecting

parts that do not vary.• Results in fewer unexpected consequences

from code change.

Page 10: Chapter 1: Introduction to Design Patterns. SimUDuck Example.

Solution

• Design principle: Program to interface and not to implementation.

• An interface FlyBehavior will be implemented by subclasses for different types of flight.

• An interface Quackable will be implemented for different types of “quack”.

Page 11: Chapter 1: Introduction to Design Patterns. SimUDuck Example.

Example

makeSound()

Animal

makeSound()

Dog

makeSound()

Cat

Abstract class OR Interface

Page 12: Chapter 1: Introduction to Design Patterns. SimUDuck Example.

FlyBehavior Interface

FlyBehavior

FlyWithWings FlyNoWay

fly()

fly()fly()

Page 13: Chapter 1: Introduction to Design Patterns. SimUDuck Example.

QuackBehavior

QuackBehavior

Quack Squeak

quack()

quack()quack()

MuteQuack

quack()

Page 14: Chapter 1: Introduction to Design Patterns. SimUDuck Example.

New Design

swim()display()performFly()performQuack()

Duck

display()

MallardDuck

display()

RedheadDuck

display()

RubberDuck

display()

DecoyDuck

fly()

FlyBehvior<<interface>>

quack()

QuackBehvior<<interface>>has a

has a

Page 15: Chapter 1: Introduction to Design Patterns. SimUDuck Example.

On Last Problem• Still programming to implementation in the

subclasses.• Solution – include set methods in the Duck

class– To set quack behavior– To set fly behavior

• The set methods allow for behavior to be dynamic at runtime

Page 16: Chapter 1: Introduction to Design Patterns. SimUDuck Example.

Exercise• Add a new type of duck to the simulator,

namely, model duck. A model duck does not fly and quacks.

• Add a new fly behavior to the simulator, namely, FlyRocketPower, which represents flight via a rocket.

• Create an instance of a model duck and change its behaviour at runtime to be flight via a rocket.

Page 17: Chapter 1: Introduction to Design Patterns. SimUDuck Example.

Has-A Can be Better than Is-A

• Inheritance promotes reuse but not extensibility.

• Composition facilitates more flexibility.• Composition allows for behavior to change at

runtime (rather than editing code).• Design principle: Favor composition over

inheritance.

Page 18: Chapter 1: Introduction to Design Patterns. SimUDuck Example.

The Strategy Pattern

Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Page 19: Chapter 1: Introduction to Design Patterns. SimUDuck Example.

Lessons Learned from the Strategy Pattern

• Encapsulate what varies.• Favor composition over inheritance.• Program to interfaces, not implementations

Page 20: Chapter 1: Introduction to Design Patterns. SimUDuck Example.

Advantages of Sharing a Pattern Vocabulary

• Shared pattern vocabularies are powerful.• Patterns allow you to say more with less.• Talking at the pattern level allows to stay “in

design” longer.• Shared vocabularies can turbo charge your

development.• Shared vocabularies encourage more junior

developers to get up to speed.