a simple duck app!

19
A Simple Duck app!

Upload: toni

Post on 23-Feb-2016

39 views

Category:

Documents


0 download

DESCRIPTION

A Simple Duck app!. Now we need ducks to FLY!. What about plastic ducks?. Inheritance solves the problem . How about interface?. Zooming in on the problem. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: A Simple Duck app!

A Simple Duck app!

Page 2: A Simple Duck app!

Now we need ducks to FLY!

Page 3: A Simple Duck app!

What about plastic ducks?

Page 4: A Simple Duck app!

Inheritance solves the problem

Page 5: A Simple Duck app!

How about interface?

Page 6: A Simple Duck app!
Page 7: A Simple Duck app!
Page 8: A Simple Duck app!

Zooming in on the problem

• Inheritance wont solve the problem since the duck behavior keeps changing across the subclasses, and its not appropriate for all subclasses to have those behaviors.

• Flyable and Quackable interface sounded promising at first- only ducks that really do fly will be Flyable, etc.. Except Java interfaces have no implementation code, so no code reuse…. And that means that whatever you need to modify, a behavior, you are forced to track down and change it in all the different subclasses where that behavior is defined…

Page 9: A Simple Duck app!

Design Principle

Identify the aspects of your application that vary and separate them from what

stays the same

Take what varies and “encapsulate” it so it wont affect the rest of your code

Page 10: A Simple Duck app!

Separate what changes…

Page 11: A Simple Duck app!

Designing Duck Behaviors

• Assigning behaviors to the instances of Duck• MallardDuck instance and initialize it with a

specific type of flying behavior. • Why not change the behavior dynamically i.e.

include behavior setter methods in the Duck classes so that we can change the MallardDuck’s flying behavior at runtime

Page 12: A Simple Duck app!

• Duck behaviors will live in a separate class- a class that implements a particular behavior interface

Page 13: A Simple Duck app!

“ Progr am to an inter face” rea l ly means “ Pr ogram to a super type ”

Page 14: A Simple Duck app!

Implementing quake behaviors

Page 15: A Simple Duck app!

Integrating Duck Behavior

• Duck will delegate flying and quacking behavior, instead of using these methods in the Duck class (or sub classes)

Page 16: A Simple Duck app!

peformQuack()

public class Duck { QuackBehavior quackBehavior;

// more

public void performQuack() PquackBehavior.quack();}

}

Each Duck has a reference to something that implements the QuackBehavior

interface

Rather than handling the

quack behavior itself, the Duck

object delegates that behavior to the object referred

by quackBehavior

Page 17: A Simple Duck app!

MallardDuck…

Public class MallardDuck extends Duck {public MallardDuck() {quackBehavior = new Quack();flyBehavior = new FlywithWings();}

public void display() { System.out.println(“I’m a real Mallard duck”);

}}

Page 18: A Simple Duck app!

Encapsulated Behaviors

Page 19: A Simple Duck app!