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