question about interfaces

F

Farcus Pottysquirt

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)
 
A

andrewmcdonagh

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();
snipped...


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)

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

Farcus Pottysqirt

andrewmcdonagh said:
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.
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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top