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
Raw Normal View History

2022-11-09 21:22:13 +00:00
module strategy.duck;
2022-11-11 09:54:25 +00:00
2022-11-09 21:22:13 +00:00
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!");
}
2022-11-09 21:29:18 +00:00
void setFlyBehavior(FlyBehavior fb)
{
flyBehavior = fb;
}
void setQuackBehavior(QuackBehavior qb)
{
quackBehavior = qb;
}
2022-11-09 21:22:13 +00:00
}
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");
}
}