Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

question about interfaces

Thread view: 
Farcus Pottysquirt - 28 Jan 2007 16:15 GMT
Sorry for the lengthiness of this post, but in order to illustrate my
question, I needed to include all the relevant classes.

I am working through a book on design patterns (Head First Design
Patterns)  and the first set of code  sets up a duck system.  The
original MallardDuck class:

public class MallardDuck extends Duck {

    /** Creates a new instance of MallardDuck */
    public MallardDuck() {

        quackBehavior = new Quack();
        flyBehavior = new FlyWithWings();
    }
    public void display()
    {
        System.out.println("I'm a real mallard duck");
    }

}

I was just curious so I added a second flyBehavior:

public class MallardDuck extends Duck {

    /** Creates a new instance of MallardDuck */
    public MallardDuck() {

        quackBehavior = new Quack();
        flyBehavior = new FlyWithWings();
        flyBehavior = new FlyNoWay();
    }
    public void display()
    {
        System.out.println("I'm a real mallard duck");
    }

}

wanting to know what would happen.

Here are the remaining pieces
---------------------------
public abstract class Duck {

    FlyBehavior flyBehavior;
    QuackBehavior quackBehavior;
    public Duck() {

    }

    public abstract void display();

    public void performFly() {
        flyBehavior.fly();
    }
    public void performQuack() {
        quackBehavior.quack();
    }
    public void swim() {
        System.out.println("All ducks float, even decoys");
    }
}

----------------------
public class FlyWithWings implements FlyBehavior {
    public void fly() {
        System.out.println("I'm flying");
    }

}
----------------------
public class FlyNoWay implements FlyBehavior{

    public void fly() {
        System.out.println("I can't fly");
    }
}

---------------------------Here is the main class -------------
public class MiniDuckSimulator {

    /** Creates a new instance of MiniDuckSimulator */
    public static void main(String [] args) {
        Duck mallard = new MallardDuck();
        mallard.performQuack();
        mallard.performFly();
    }

}
-----------------------------------------
I wasn't quite sure what would happen with the second reference variable
 flyBehavior.  When I ran the program,  I get this

init:
deps-jar:
compile-single:
run-single:
Quack
I'm flying
BUILD SUCCESSFUL (total time: 1 second)

Why would it not show the results from both flyBehavior references like such

init:
deps-jar:
compile-single:
run-single:
Quack
I'm flying
I can't fly
BUILD SUCCESSFUL (total time: 1 second)
andrewmcdonagh - 28 Jan 2007 16:46 GMT
On Jan 28, 4:15 pm, Farcus Pottysquirt <where_is_my_...@movies.net>
wrote:
> Sorry for the lengthiness of this post, but in order to illustrate my
> question, I needed to include all the relevant classes.
[quoted text clipped - 26 lines]
>          flyBehavior = new FlyWithWings();
>          flyBehavior = new FlyNoWay();

snipped...

> Why would it not show the results from both flyBehavior references like such
>
[quoted text clipped - 6 lines]
> I can't fly
> BUILD SUCCESSFUL (total time: 1 second)

Because there is only one  'flyBehaviour' reference and it was over
written to point to an instance of the 'FlyNoWings' class.

To get both behaviours, you would could over ride the
Duck.performFly() method to call fly() on two 'flyBehaviour classes.

e.g.
public class MallardDuck extends Duck {

   private FlyBehaviour flyBehaviour1;
   private FlyBehaviour flyBehaviour2;

    /** Creates a new instance of MallardDuck */
    public MallardDuck() {
        quackBehavior = new Quack();
        flyBehavior1 = new FlyWithWings();
        flyBehavior2 = new FlyNoWay();
    }

    public void performFly() {
        flyBehavior1.fly();
        flybehaviour2.fly();
    }

    public void display()   {
        System.out.println("I'm a real mallard duck");
    }

}

Alternatively, you could use the Decorator Design Pattern (page 88 of
your book).....
Farcus Pottysqirt - 29 Jan 2007 00:07 GMT
> Because there is only one  'flyBehaviour' reference and it was over
> written to point to an instance of the 'FlyNoWings' class.

So based on this, would you say that the following evaluation of the
"process" is correct?

<interpretation>
An object of type Duck is instantiated as a new Mallard Duck
A mallard duck inherits its:
    flyBehavior : reference variable
    quackBehavior : reference variable
    performFly : method
    performQuack : method
from the abstract class  Duck
It has it's own swim method

A mallard duck overrides the display method
The MallardDuck constructor sets
    quackBehavior = Quack
    flyBehavior = FlyWithWings

When the new mallard is created, the call to performQuack calls Quack
                      the call to performFly calls flyWithWings

The quackBehavior interface has one empty method called quack which must
be implemented by every class that implements the interface.
</interpretation>

> To get both behaviours, you would could over ride the
> Duck.performFly() method to call fly() on two 'flyBehaviour classes.
>
> Alternatively, you could use the Decorator Design Pattern (page 88 of
> your book).....

I haven't gotten that far yet, but I'm working on it  :-)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.