static void Main(string[] args) { IFlyBehaviour flyNoWay = new FlyNoWay(); IFlyBehaviour flyWithWings = new FlyWithWings(); Duck mallardDuck = new MallardDuck(); mallardDuck.SetFlyBehaviour(flyWithWings); Duck redheadDuck = new RedheadDuck(); redheadDuck.SetFlyBehaviour(flyWithWings); Duck rubberDuck = new RubberDuck(); rubberDuck.SetFlyBehaviour(flyNoWay); Duck decoyDuck = new DecoyDuck(); decoyDuck.SetFlyBehaviour(flyNoWay); List <Duck> listDucks = new List <Duck> { mallardDuck, redheadDuck, rubberDuck, decoyDuck }; foreach (var duck in listDucks) { duck.Fly(); } }
public void ShouldGetRubberDuckCanSwimButNotFlyAndSqueakQuack() { var quack = new RubberDuck(); quack.SetFlyBehaviour(new FlyNoWay()); quack.SetQuackBehaviour(new Squeak()); quack.Display(); quack.PerformQuack(); quack.Swim(); quack.Display(); }
internal static void ExecuteStrategy() { //Intent // • Define a family of algorithms, encapsulate each one, and make them interchangeable.Strategy lets the algorithm vary independently from // the clients that use it. // • Capture the abstraction in an interface, bury implementation details in derived classes. Strategy.Duck greenDuck = new GreenDuck(); greenDuck.PerformFly(); greenDuck.PerformQuack(); Strategy.Duck rubberDuck = new RubberDuck(); rubberDuck.PerformFly(); rubberDuck.PerformQuack(); rubberDuck.SetFlyBehaviour(new FlyWithRockets()); // Changing behaviour at runtime ! rubberDuck.PerformFly(); }