//Design principle //Identify the aspects of your //application that vary and separate //them from what stays the same. //Design principle //Program to an interface (abstract class/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. static void Main(string[] args) { MallardDuck mallardDuck = new MallardDuck(); mallardDuck.PerformFly(); mallardDuck.SetFlyBehavior(new FlyRocketPowered()); mallardDuck.PerformFly(); mallardDuck.PerformQuack(); mallardDuck.Display(); mallardDuck.Swim(); }
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 mallard = new MallardDuck(); mallard.Display(); mallard.PerformQuack(); mallard.PerformFly(); Duck fauxDuck = new ModelDuck(); fauxDuck.Display(); fauxDuck.PerformQuack(); fauxDuck.PerformFly(); Duck disabledMallard = new MallardDuck() { FlyBehavior = new CannotFlyBehavior(), QuackBehavior = new MuteBehavior() }; disabledMallard.Display(); disabledMallard.PerformFly(); disabledMallard.PerformQuack(); fauxDuck.FlyBehavior = new JetPackBehavior(); fauxDuck.Display(); fauxDuck.PerformFly(); Console.ReadLine(); }
static void Main(string[] args) { Duck mallard = new MallardDuck(); mallard.Display(); mallard.PerformQuack(); mallard.PerformFly(); Duck fauxDuck = new ModelDuck(); fauxDuck.Display(); fauxDuck.PerformQuack(); fauxDuck.PerformFly(); Duck disabledMallard = new MallardDuck() { FlyBehavior = new CannotFlyBehavior(), QuackBehavior = new MuteBehavior() }; disabledMallard.Display(); disabledMallard.PerformFly(); disabledMallard.PerformQuack(); fauxDuck.FlyBehavior = new JetPackBehavior(); fauxDuck.Display(); fauxDuck.PerformFly(); Console.ReadLine(); }
static void Main(string[] args) { Duck mallard = new MallardDuck(); mallard.Display(); mallard.PerformQuack(); mallard.PerformFly(); Console.WriteLine(); Duck model = new ModelDuck(); model.Display(); model.PerformQuack(); model.PerformFly(); model.SetFlyBehaviour(new FlyRocketPowered()); model.PerformFly(); Console.WriteLine(); Duck decoy = new DecoyDuck(); decoy.Display(); decoy.PerformQuack(); decoy.PerformFly(); decoy.SetQuackBehaviour(new Squeak()); Console.WriteLine(); Duck rubber = new RubberDuck(); rubber.Display(); rubber.PerformQuack(); rubber.PerformFly(); rubber.Swim(); Console.WriteLine(); }
static void Main(string[] args) { DuckBase duck1 = new MallardDuck("Mallard Duck"); duck1.Display(); duck1.Swim(); duck1.PerformFly(); duck1.PerformQuack(); Console.WriteLine(); DuckBase duck2 = new DecoyDuck("Decoy Duck"); duck2.Display(); duck2.Swim(); duck2.PerformFly(); duck2.PerformQuack(); Console.WriteLine(); DuckBase duck3 = new RubberDuck("Rubber Duck"); duck3.Display(); duck3.Swim(); duck3.PerformFly(); duck3.PerformQuack(); }
static void Main(string[] args) { Duck mallard = new MallardDuck(); mallard.Display(); mallard.PerformFly(); mallard.PerformQuack(); }
static void Main(string[] args) { Duck mallard = new MallardDuck(); mallard.PerformQuack(); mallard.PerformFly(); Console.ReadLine(); }
static void Main(string[] args) { Duck mallard = new MallardDuck(); mallard.PerformFly(); mallard.PerformQuak(); mallard.Display(); mallard.Swim(); Console.ReadKey(); }
private void CreateMallardDuckWithRocketAndSqueak() { Duck mallardDuck = new MallardDuck(); mallardDuck.SetFlyBehavior(new FlyRocketPowered()); mallardDuck.SetQuackBehavior(new Squeak()); mallardDuck.Display(); mallardDuck.PerformFly(); mallardDuck.PerformQuack(); }
static void Main(string[] args) { Duck mallard = new MallardDuck(); mallard.PerformFly(); mallard.PerformQuak(); mallard.Display(); mallard.Swim(); Console.ReadKey(); }
static void Main(string[] args) { Duck myDuck = new MallardDuck(); myDuck.Display(); myDuck.PerformFly(); myDuck.PerformQuack(); myDuck.SetFlyBehavior(new FlyWithWingsTypeTwo()); myDuck.SetQuackBehavior(new NoQuack()); myDuck.PerformFly(); myDuck.PerformQuack(); myDuck = new RubberDuck(); myDuck.Display(); myDuck.PerformFly(); myDuck.PerformQuack(); myDuck.SetFlyBehavior(new FlyWithJet()); myDuck.SetQuackBehavior(new QuackLikeMallardDuck()); myDuck.PerformFly(); myDuck.PerformQuack(); }
static void Main(string[] args) { Duck mallardDuck = new MallardDuck(); mallardDuck.PerformFly(); mallardDuck.PerformQuack(); Duck plasticDuck = new PlasticDuck(); plasticDuck.PerformFly(); plasticDuck.PerformQuack(); }
static void Main() { Duck mallard = new MallardDuck(); mallard.PerformQuack(); mallard.PerformFly(); Duck model = new ModelDuck(); model.PerformFly(); model.SetFlyBehavior(new FlyRocketPowered()); model.PerformFly(); }
static void Main(string[] args) { Duck mallard = new MallardDuck(); mallard.PerformQuack(); mallard.PerformFly(); Duck model = new ModelDuck(); model.PerformFly(); model.FlyBehavior = new FlyRocketPowered(); model.PerformFly(); }
static void Main(string[] args) { var mallard = new MallardDuck(); mallard.PerformFly(); mallard.PerformQuack(); var modelDuck = new ModelDuck(); modelDuck.PerformQuack(); modelDuck.PerformFly(); modelDuck.SetFlyBehavior(new FlyRocketPowered()); modelDuck.PerformFly(); }
static void Main(string[] args) { Duck mallard = new MallardDuck(); mallard.PerformQuack(); mallard.PerformFly(); Duck model = new ModelDuck(); model.PerformFly(); model.SetFlyBehavior(new FlyRocketPowered()); model.PerformFly(); Console.ReadKey(); }
private static void MiniDuckSimulator() { var mallard = new MallardDuck(); mallard.Display(); mallard.PerformFly(); mallard.PerformQuack(); var model = new ModelDuck(); model.Display(); model.PerformFly(); model.PerformQuack(); Console.WriteLine("---------\r\n>>> powered by rocket."); model.SetFlyBehavior(new FlyRocketPowered()); model.PerformFly(); }
/** The Strategy Pattern defines a family of algorithms, * encapsulates each one, and makes them interchangeable. * Strategy lets the algortihm vary independently from * clients that use it. * * In this case it allows to quickly add new properties * like allow different ducks to swim, each of them could * swim differently and so on */ static void Main(string[] args) { var mallard = new MallardDuck(); mallard.Display(); mallard.PerformFly(); mallard.PerformQuack(); Console.WriteLine(); var displayDuck = new DisplayDuck(); displayDuck.Display(); displayDuck.PerformFly(); displayDuck.PerformQuack(); Console.WriteLine(); displayDuck.SetFly(new FlyWithWings()); displayDuck.PerformFly(); }
static void Main(string[] args) { Duck Mallard = new MallardDuck(); Mallard.Display(); Mallard.PerformQuack(); Mallard.PerformFly(); Duck Model = new ModelDuck(); Console.WriteLine(); Model.Display(); Model.PerformQuack(); Model.PerformFly(); Model.SetFlyBehavior(new FlyRocketPowered()); Model.PerformFly(); Duck Redhead = new RedheadDuck(); Console.WriteLine(); Redhead.Display(); Redhead.PerformQuack(); Redhead.PerformFly(); Duck Rubber = new RubberDuck(); Console.WriteLine(); Rubber.Display(); Rubber.PerformQuack(); Rubber.PerformFly(); Duck Decoy = new DecoyDuck(); Console.WriteLine(); Decoy.Display(); Decoy.PerformQuack(); Decoy.PerformFly(); }