static void Main(string[] args) { Console.WriteLine("-> Testing different ducks behaviors."); var mallard = new MallardDuck(); mallard.Display(); mallard.PerformQuack(); mallard.PerformFly(); var redhead = new RedheadDuck(); redhead.Display(); redhead.PerformQuack(); redhead.PerformFly(); var rubber = new RubberDuck(); rubber.Display(); rubber.PerformQuack(); rubber.PerformFly(); var decoy = new DuckDecoy(); decoy.Display(); decoy.PerformQuack(); decoy.PerformFly(); Console.WriteLine(); Console.WriteLine("-> Test runtime behavior change."); var model = new ModelDuck(); model.Display(); model.PerformQuack(); model.PerformFly(); model.FlyBehavior = new FlyRocketPowered(); Console.WriteLine("Fly behavior of ModelDuck was changed and now it's..."); model.PerformFly(); Console.WriteLine(); Console.WriteLine("-> Class not in duck hierarchy using duck behavior."); var soundSynthesizer = new SoundSynthesizer(); soundSynthesizer.DuckSound(); Console.WriteLine(); Console.WriteLine("Press any key to continue. . ."); Console.ReadKey(); }
static void Main(string [] args) { MallardDuck mallardDuck = new MallardDuck(); PlayWithDuck(mallardDuck); RedheadDuck redheadDuck = new RedheadDuck(); PlayWithDuck(redheadDuck); RubberDuck rubberDuck = new RubberDuck(); PlayWithDuck(rubberDuck); DecoyDuck decoyDuck = new DecoyDuck(); PlayWithDuck(decoyDuck); ModelDuck modelDuck = new ModelDuck(); PlayWithDuck(modelDuck); modelDuck.SetFlyBehavior(FlyBehavior.FlyWithWingsAction()); PlayWithDuck(modelDuck); }
static void Main(string[] args) { // Create Instances of Strategies and Contexts. Squeak theSqueak = new Squeak (); Quack theQuack = new Quack (); MuteQuack theMuteQuack = new MuteQuack (); FlyWithWings theFly = new FlyWithWings (); Flyless theFlyless = new Flyless (); Duck magicDuck = new MallardDuck(theQuack, theFly); Duck rorschachDuck = new RedheadDuck (theMuteQuack, theFlyless); // Call functions. magicDuck.display(); magicDuck.performQuack(); magicDuck.performFly(); Console.Out.WriteLine (); rorschachDuck.display(); rorschachDuck.performQuack(); rorschachDuck.performFly(); Console.Out.WriteLine (); // Assign new Strategies to Instances. magicDuck.QuackBehaviour = theSqueak; magicDuck.FlyBehaviour = theFlyless; rorschachDuck.QuackBehaviour = theQuack; rorschachDuck.FlyBehaviour = theFly; // Call functions. magicDuck.display(); magicDuck.performQuack(); magicDuck.performFly(); Console.Out.WriteLine (); rorschachDuck.display(); rorschachDuck.performQuack(); rorschachDuck.performFly(); Console.In.ReadLine(); }
static void Main(string[] args) { // Create Instances of Strategies and Contexts. Squeak theSqueak = new Squeak(); Quack theQuack = new Quack(); MuteQuack theMuteQuack = new MuteQuack(); FlyWithWings theFly = new FlyWithWings(); Flyless theFlyless = new Flyless(); Duck magicDuck = new MallardDuck(theQuack, theFly); Duck rorschachDuck = new RedheadDuck(theMuteQuack, theFlyless); // Call functions. magicDuck.display(); magicDuck.performQuack(); magicDuck.performFly(); Console.Out.WriteLine(); rorschachDuck.display(); rorschachDuck.performQuack(); rorschachDuck.performFly(); Console.Out.WriteLine(); // Assign new Strategies to Instances. magicDuck.QuackBehaviour = theSqueak; magicDuck.FlyBehaviour = theFlyless; rorschachDuck.QuackBehaviour = theQuack; rorschachDuck.FlyBehaviour = theFly; // Call functions. magicDuck.display(); magicDuck.performQuack(); magicDuck.performFly(); Console.Out.WriteLine(); rorschachDuck.display(); rorschachDuck.performQuack(); rorschachDuck.performFly(); Console.In.ReadLine(); }