I am working on going through "Head First Design Patterns" and I am trying to properly convert the Java code into C#. Here is what I have. Can you tell me if this is a good implementation/conversion so far?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Duck
{
public abstract class Duck
{
public FlyBehavior flyBehavior;
public QuackBehavior quackBehavior;
public Duck(){
}
public abstract void display();
public void performFly()
{
flyBehavior.fly();
}
public void performQuack()
{
quackBehavior.quack();
}
public void swim()
{
Console.WriteLine("All ducks float, even decoys!");
}
}
public interface FlyBehavior
{
void fly();
}
public class FlyWithWings : FlyBehavior
{
public void fly()
{
Console.WriteLine("I'm flying!");
}
}
public class FlyNoWay : FlyBehavior
{
public void fly()
{
Console.WriteLine("I can't fly!");
}
}
public interface QuackBehavior
{
void quack();
}
public class Quack : QuackBehavior
{
public void quack()
{
Console.WriteLine("Quack");
}
}
public class MuteQuack : QuackBehavior
{
public void quack()
{
Console.WriteLine("<< Silence >>");
}
}
public class Squeak : QuackBehavior
{
public void quack()
{
Console.WriteLine("Squeak");
}
}
public class MallardDuck : Duck
{
public MallardDuck()
{
quackBehavior = new Quack();
flyBehavior = new FlyWithWings();
}
public override void display()
{
Console.WriteLine("I'm a real Mallard duck");
}
}
public class MiniDuckSimulator
{
public static void Main(string[] args)
{
Duck mallard = new MallardDuck();
mallard.performQuack();
mallard.performFly();
Console.ReadLine();
}
}
}