コード例 #1
0
        private static void Main()
        {
            var turkey      = new WildTurkey();
            var duck        = new MallardDuck();
            var adapter     = new TurkeyAdapter(turkey);
            var duckadapter = new DuckAdapter(duck);

            Tester(adapter);
            Tester(duckadapter);
        }
コード例 #2
0
        static void Main(string[] args)
        {
            var duck = new MallardDuck();

            var turkey        = new WildTurkey();
            var turkeyAdapter = new TurkeyAdapter(turkey);


            Console.WriteLine("The Turkey says...");
            turkey.Gobble();
            turkey.Fly();

            Console.WriteLine("The Duck says...");
            TestDuck(turkeyAdapter);

            var duckAdapter = new DuckAdapter(duck);

            TestTurkey(duckAdapter);
        }
コード例 #3
0
        static void Main(string[] args)
        {
            //Mallard duck is the target class which is derived from IDuck
            //WildTurkey is the adaptee derived from ITurkey
            //TurkeyAdapter is the adapter which adapts to IDuck which client knows so implements IDuck
            //But since the behaviour is of type WildTurkey an instance of  WildTurkey is passed in the constuctor.

            /*If we have a third party API and client does not know about these api's then we can come up with the class
             * which is derived from the class known to the client and provide a constructor which takes the third party API instance
             * Each of the methods in the known class will be modified to call the third pary api.*/

            Console.WriteLine("\nThis is Mallard actions:");
            IDuck mallardDuck = new MallardDuck();

            mallardDuck.Quack();
            mallardDuck.Fly();

            Console.WriteLine("\nThis is Wild Turkey actions:");
            ITurkey wildTurkey = new WildTurkey();

            wildTurkey.Gobble();
            wildTurkey.Fly();

\
            IDuck turkeyAdapter = new TurkeyAdapter(wildTurkey);

            Console.WriteLine("\nThis is Duck Adapter actions:");
            TestDuck(turkeyAdapter);


            DuckAdapter duckAdapter = new DuckAdapter(mallardDuck);

            Console.WriteLine("\nThis is Turkey Adapter actions:");
            TestDuck(duckAdapter);

            IDuck classAdapter = new ClassAdapter(wildTurkey);

            Console.WriteLine("\nThis is class Adapter actions:");
            TestDuck(classAdapter);

            Console.ReadLine();
        }