wood burning stoves 2.0*
The moose likes OO, Patterns, UML and Refactoring and the fly likes HFDS doubt? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "HFDS doubt?" Watch "HFDS doubt?" New topic
Author

HFDS doubt?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9243
    
    1

Guys,

I'm not able to understand the following from the HFDS book! Page 21 says that

Duck model = new ModelDuck();
model.performFly();
model.setFlyBehavior(new FlyRocketPowered());
model.performFly();

And there is a small comment below it that says "If it worked, the model duck dynamically changed its flying behavior! You can't do that is the implementation lives inside the duck class." Can anyone justify this me please?? What they mean by saying "implementation lives inside the duck class"

Help needed please!


SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If the ModelDuck performFly() method had code to flap wings you couldn't change how a Duck flies without changing the ModelDuck class itself. I'm sure that's what they meant by "implementation lives within the duck class."

But ModelDuck apparently delegate its flying to a FlyBehavior class. The default FlyBehavior might well flap wings. But in that sample code snippet we plug in a brand new version of FlyBehavior with rocket packs. Now our Duck flies with rockets.

We can add new behaviors without touching the Duck class, and we can change the FlyBehavior as often as we want in a running program. Very cool stuff!

Makes me think of the Matrix where somebody could download the skills to fly a helicopter.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9243
    
    1

Thanks stan for the explanation. I'm close to understanding it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HFDS doubt?
 
Similar Threads
Polymorphism is the deebil
Defining a Class
city name
The Ducks are back
Casting HashMap.keySet()