66 lines
1009 B
D
66 lines
1009 B
D
module strategy.duck;
|
|
import strategy.flybehavior;
|
|
import strategy.quackbehavior;
|
|
import std.stdio : writeln;
|
|
|
|
abstract class Duck
|
|
{
|
|
FlyBehavior flyBehavior;
|
|
QuackBehavior quackBehavior;
|
|
|
|
void display();
|
|
|
|
void performFly()
|
|
{
|
|
flyBehavior.fly();
|
|
}
|
|
|
|
void performQuack()
|
|
{
|
|
quackBehavior.quack();
|
|
}
|
|
|
|
void swim()
|
|
{
|
|
writeln("All ducks float, even decoys!");
|
|
}
|
|
|
|
void setFlyBehavior(FlyBehavior fb)
|
|
{
|
|
flyBehavior = fb;
|
|
}
|
|
|
|
void setQuackBehavior(QuackBehavior qb)
|
|
{
|
|
quackBehavior = qb;
|
|
}
|
|
}
|
|
|
|
class MallardDuck : Duck
|
|
{
|
|
this()
|
|
{
|
|
quackBehavior = new Quack;
|
|
flyBehavior = new FlyWithWings;
|
|
}
|
|
|
|
override void display()
|
|
{
|
|
writeln("I'm a real Mallard duck");
|
|
}
|
|
}
|
|
|
|
class ModelDuck : Duck
|
|
{
|
|
this()
|
|
{
|
|
quackBehavior = new Squeak;
|
|
flyBehavior = new FlyNoWay;
|
|
}
|
|
|
|
override void display()
|
|
{
|
|
writeln("I'm a model duck");
|
|
}
|
|
}
|