- Strategy Pattern SimUDuck Introduction
- SimUDuck WPF Code
This post carries on from the previous post, but it completes most of the code in C# in the first chapter of Head First Design Patterns. Feel free to go ahead and copy this C# code and experiment with it.
I named my C# WPF .NET project SimUDuck2. Below are the code listings.
<Window x:Class="SimUDuck2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:SimUDuck2" mc:Ignorable="d" Title="SimUDuck2 HeadFirst Design Patterns" Height="350" Width="500"> <StackPanel Margin="10"> <TextBlock>Chapter One - Intro</TextBlock> <TextBlock x:Name="txtBlk"></TextBlock> <TextBlock x:Name="txtBlk2"></TextBlock> <TextBlock x:Name="txtBlk3"></TextBlock> <TextBlock x:Name="txtBlk4"></TextBlock> <TextBlock x:Name="txtBlk5"></TextBlock> <TextBlock x:Name="txtBlk6"></TextBlock> <TextBlock x:Name="txtBlk7"></TextBlock> <TextBlock x:Name="txtBlk8"></TextBlock> <TextBlock x:Name="txtBlk9"></TextBlock> <TextBlock x:Name="txtBlk10"></TextBlock> <TextBlock x:Name="txtBlk11"></TextBlock> <TextBlock x:Name="txtBlk12"></TextBlock> <TextBlock x:Name="txtBlk13"></TextBlock> </StackPanel> </Window>
The code behind.
using System.Windows; namespace SimUDuck2 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); MallardDuck mallard = new MallardDuck(); txtBlk.Text = mallard.display(); txtBlk2.Text = mallard.performFly(); txtBlk3.Text = mallard.swim(); txtBlk4.Text = mallard.performQuack(); RubberDuck rubber = new RubberDuck(); txtBlk6.Text = rubber.display(); txtBlk7.Text = rubber.performFly(); txtBlk8.Text = rubber.performQuack(); ModelDuck model = new ModelDuck(); txtBlk10.Text = model.display(); txtBlk11.Text = model.performFly(); model.setFlyBehavior(new FlyRocketPowered()); txtBlk12.Text = (model.performQuack() + " !!!").ToUpper(); txtBlk13.Text = model.performFly(); } } }
IFlyBehavior.cs
namespace SimUDuck2 { public interface IFlyBehavior { public string fly(); } }
FlyWithWings.cs
namespace SimUDuck2 { public class FlyWithWings : IFlyBehavior { public string fly() { return "flying with wings... "; } } }
FlyNoWay.cs
namespace SimUDuck2 { public class FlyNoWay : IFlyBehavior { public string fly() { return "no fly"; // not able to fly } } }
FlyRocketPowered.cs
namespace SimUDuck2 { public class FlyRocketPowered : IFlyBehavior { public string fly() { return "I'm flying like a rocket!"; } } }
IQuackBehavior.cs
namespace SimUDuck2 { public interface IQuackBehavior { public string quack(); } }
Quack.cs
namespace SimUDuck2 { class Quack : IQuackBehavior { public string quack() { return "quack"; } } }
Squeak.cs
namespace SimUDuck2 { public class Squeak : IQuackBehavior { public string quack() { return "squeak squeak"; } } }
Duck.cs
namespace SimUDuck2 { public abstract class Duck { protected IFlyBehavior flyBehavior; protected IQuackBehavior quackBehavior; public string swim() { return "duck is swim swim swimming..."; } public string performFly() { return flyBehavior.fly(); } public string performQuack() { return quackBehavior.quack(); } public void setFlyBehavior(IFlyBehavior fb) { flyBehavior = fb; } public void setQuackBehavior(IQuackBehavior qb) { quackBehavior = qb; } } }
MallardDuck.cs
namespace SimUDuck2 { class MallardDuck : Duck { public MallardDuck() { flyBehavior = new FlyWithWings(); // polymorphism quackBehavior = new Quack(); } public string display() { return "displaying Mallard Duck... "; } } }
RubberDuck.cs
namespace SimUDuck2 { class RubberDuck : Duck { public RubberDuck() { flyBehavior = new FlyNoWay(); quackBehavior = new Squeak(); } public string display() { return "displaying Rubber Duck... "; } } }
ModelDuck.cs
namespace SimUDuck2 { public class ModelDuck : Duck { public ModelDuck() { flyBehavior = new FlyNoWay(); quackBehavior = new Quack(); } public string display() { return "displaying Model Duck... "; } } }