static void Main(string[] args) { /* * Design principle: Identify the aspects of your application that vary and separate them from what stays the same. * * Design principle: Program to an interface, not an implementation. * * Design principle: Favor composition over inheritance. * * The Strategy pattern: defines a family of algorithms, encapsulates each one, and makes them interchangeable. * Strategy lets the algorithm vary independently from clients that use it. */ Duck mallard = new MallardDuck(); mallard.display(); mallard.PerformQuack(); mallard.PerformFly(); Duck model = new ModelDuck(); model.display(); model.PerformQuack(); model.PerformFly(); model.SetFlyBehavior(new FlyRocketPowered()); model.PerformFly(); Console.ReadKey(); }
static void Main(string[] args) { Duck myduck = new MallardDuck(); myduck.display(); myduck.performFly(); myduck.performQuack(); myduck = new RedHeadDuck(); myduck.display(); myduck.performFly(); myduck.performQuack(); myduck = new RubberDuck(); myduck.display(); myduck.performFly(); myduck.performQuack(); myduck = new DecoyDuck(); myduck.display(); myduck.performFly(); myduck.performQuack(); myduck.setFlyBehavior(new FlyJet()); myduck.performFly(); }
static void Main(string[] args) { Duck num1 = new MallardDuck(); num1.display(); num1.performFly(); num1.performQuack(); num1.Swim(); Duck num2 = new RedheadDuck(); num2.display(); num2.performFly(); num2.performQuack(); num2.Swim(); Duck num3 = new DecoyDuck(); num3.display(); num3.performFly(); num3.performQuack(); num3.Swim(); num3.setFlyBehavior(new FlyWithWings()); num3.performFly(); Console.Read(); }