1 / 19

A Simple Duck app!

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.

toni
Download Presentation

A Simple Duck app!

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Simple Duck app!

  2. Now we need ducks to FLY!

  3. What about plastic ducks?

  4. Inheritance solves the problem 

  5. How about interface?

  6. 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…

  7. 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

  8. Separate what changes…

  9. 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

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

  11. “Program to an interface” really means “Program to a super type”

  12. Implementing quake behaviors

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

  14. peformQuack() Each Duck has a reference to something that implements the QuackBehavior interface public class Duck { QuackBehaviorquackBehavior; // more public void performQuack() P quackBehavior.quack(); } } Rather than handling the quack behavior itself, the Duck object delegates that behavior to the object referred by quackBehavior

  15. 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”); } }

  16. Encapsulated Behaviors

More Related