static void Main(string[] args) { Duck mallard = new MallardDuck(); mallard.PerformQuack(); mallard.PerformFly(); Duck modelDuck = new ModelDuck(); modelDuck.PerformFly(); modelDuck.FlyBehavior = new FlyRocketPowered(); modelDuck.PerformFly(); }
static void Main(string[] args) { Duck mallardDuck = new MallardDuck(); mallardDuck.PerformQuack(); mallardDuck.PerformFly(); Duck model = new ModelDuck(); model.PerformFly(); model.setFlyBehavior(new FlyRocketPowered()); model.PerformFly(); }
static void Main(string[] args) { Duck mallard = new MallardDuck(); // Constructs new MallardDuck object mallard.performQuack(); mallard.performFly(); Duck model = new ModelDuck(); // Constructs new ModelDuck object model.performFly(); // initially calls default Fly() method defined in ModelDuck subclass to set behavior model.setFlyBehavior(new FlyRocketPowered()); // Overrides default Fly() behavior model.performFly(); }
static void Main(string[] args) { Duck duck = new MallardDuck(new FlyWithWings(), new NormalQuack()); Console.WriteLine("Mallard:"); duck.Display(); duck.PerformFly(); duck.PerformQuack(); Console.WriteLine("\nDuck Call:"); DuckCall duckCall = new DuckCall(); duckCall.PerformQuack(); }
static void Main(string[] args) { Duck mallard = new MallardDuck(); mallard.performFly(); mallard.performQuack(); ModelDuck modelDuck = new ModelDuck(); modelDuck.performFly(); modelDuck.setFlyBehavior(new FlyRocketPowered()); modelDuck.performFly(); Console.ReadKey(); }
static void Main(string[] args) { Duck mallatd = new MallardDuck(); mallatd.performQuack(); mallatd.performFly(); Duck model = new MallardDuck(); model.performFly(); //Динамическое изменение объекта model.setFlyBehavior(new FlyRocketPowered()); model.performFly(); }
static void Main() { Duck mallard = new MallardDuck(); mallard.PerformQuack(); mallard.PerformFly(); Duck model = new ModelDuck(); model.PerformFly(); model.FlyBehavior = new FlyRocketPowered(); model.PerformFly(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); }
static void Main(string[] args) { Duck mallardDuck = new MallardDuck(); mallardDuck.PerformFly(); mallardDuck.PerformQuack(); Console.WriteLine("------------------------------"); Duck modelDuck = new ModelDuck(); modelDuck.PerformFly(); modelDuck.SetFlyBehavior(new FlyWithWings()); modelDuck.PerformFly(); }
static void Main() { Duck duck = new MallardDuck(); duck.Display(); duck.PerformFly(); duck.PerformSwim(); duck = new RubberDuck(); duck.Display(); duck.PerformFly(); duck.PerformSwim(); Console.ReadLine(); }
public static void Main() { Duck mallard = new MallardDuck(); Duck rubberDuckie = new RubberDuck(); Duck decoy = new DecoyDuck(); Duck model = new ModelDuck(); mallard.PerformQuack(); rubberDuckie.PerformQuack(); decoy.PerformQuack(); mallard.PerformFly(); model.PerformFly(); model.SetFlyBehavior(new FlyRocketPowered()); model.PerformFly(); }
static void Main(string[] args) { Duck duck = new MallardDuck(); duck.PerformFly(); duck.PerformQuack(); duck.Display(); duck = new ModelDuck(); duck.Display(); duck.PerformFly(); duck.PerformQuack(); duck.SetFlyBehavior(new FlyWithRockets()); duck.PerformFly(); Console.ReadLine(); }
static void Main(string[] args) { Duck mallard = new MallardDuck(); mallard.performQuack(); mallard.performFly(); // No fly, mute Duck model = new ModelDuck(new FlyNoWay(), new MuteQuack()); model.performQuack(); model.performFly(); // Change at runtime to quack and fly rocket powered model.setQuackBehavior(new Quack()); model.setFlyBehavior(new FlyRocketPowered()); model.performQuack(); model.performFly(); }
static void Main(string[] args) { Duck mallard = new MallardDuck(); mallard.Display(); mallard.PerformQuack(); mallard.PerformFly(); Console.WriteLine(""); Duck model = new ModelDuck(); model.Display(); model.PerformFly(); model.FlyBehavior = new FlyRocketPowered(); model.PerformFly(); // Wait for user input Console.ReadKey(); }
public static void Main() { Duck duck1 = new MallardDuck(); Duck duck2 = new ModelDuck(); duck1.Display(); duck1.Swim(); duck1.PerformQuack(); duck1.PerformFly(); Console.WriteLine(); duck2.Display(); duck2.Swim(); duck2.PerformQuack(); duck2.PerformFly(); duck2.FlyBehavior = new FlyRocketPowered(); duck2.PerformFly(); Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine("*** Strategy ***\n"); Console.WriteLine("=> Mallard Duck"); Duck mallard = new MallardDuck(); mallard.PerformQuack(); mallard.PerformFly(); Console.WriteLine(); Console.WriteLine("=> Model Duck"); Duck model = new ModelDuck(); model.PerformFly(); model.FlyBehavior = new FlyRockedPowered(); model.PerformFly(); Console.WriteLine(); #if (!vscode) // Add this for run from VS in order to console window will keep open Console.WriteLine("Press Enter for exit"); Console.ReadLine(); #endif }
static void Main(string[] args) { /* * The Strategy Pattern * * Defines a family of algorithms, encapsulates each one, and makes them interchangable. * Strategy lets the algorithm vary dependently from clients that use it. * * Solves problems: * -behaviors defined at compilation not runtime * -decereased code flexibility * -duplicate the code * * Allows: * -interchangeable algorithms for behavior for each object * -change behaviors at runtime * -common behaviors code can be reused as behaviours can be shared * -Favor composition over inheritance. (Design Principle #3) * -Program to an interface not implementation. (Design Principle #2) * -Identify the aspect of your code that vary and separate them from what stays the same.[Encapsulate What Varies] (Design Principle #1) * */ //This is a Duck simulator of different kinds of ducks var mallard = new MallardDuck(); //can swim, quack, fly mallard.Display(); mallard.Swim(); mallard.Fly(); mallard.Quack(); Console.WriteLine(); var redhead = new RedheadDuck(); //can swim, quack, fly redhead.Display(); redhead.Swim(); redhead.Fly(); redhead.Quack(); Console.WriteLine(); var decoy = new DecoyDuck(); //can swim only decoy.Display(); decoy.Swim(); decoy.Fly(); decoy.Quack(); Console.WriteLine(); var rubber = new RubberDuck(); //can swim and squeek, can't fly rubber.Display(); rubber.Swim(); rubber.Fly(); rubber.Quack(); Console.WriteLine(); var model = new ModelDuck(); //can swim, can fake quack, can't fly unless defined model.Display(); model.Swim(); model.Fly(); model.SetFlyBehavoir(new FlyRocketPowered()); model.Fly(); model.Quack(); Console.ReadLine(); }