This repository has been archived on 2022-11-20. You can view files and clone it, but cannot push or open issues or pull requests.
patterns/strategy/duck.d

67 lines
1010 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");
}
}