コード例 #1
0
        static void Main(string[] args)
        {
            GreenDuck duck = new GreenDuck();

            duck.Quack();
            duck.Fly();

            Console.WriteLine("-----母鸡适配器--------");
            Duck henAdapter = new HenAdapter(new Hen());

            henAdapter.Quack();
            henAdapter.Fly();

            Console.ReadKey();
        }
コード例 #2
0
        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();
        }