Exemple #1
0
        static void Main(string[] args)
        {
            Animal animal    = new Animal();
            Animal bird      = new Bird();
            Animal blackbird = new Blackbird();
            Animal sparrow   = new Sparrow();

            animal.MakeSound();
            bird.MakeSound();
            blackbird.MakeSound();
            sparrow.MakeSound();

            Console.WriteLine("-------------------------");

            Bird blackbird2 = new Blackbird();
            Bird sparrow2   = new Sparrow();

            blackbird2.MakeSound();
            sparrow2.MakeSound();

            ///* This gives an error, you can't assign classes as its siblings, only parents. */
            //Blackbird sparrow3 = new Sparrow();


            Console.ReadLine();
        }
Exemple #2
0
        public static void Perform()
        {
            IBird    _sparrow = new Sparrow();
            IToyDuck _toyDuck = new PlasticToyDuck();
            // Wrap a bird in a birdAdapter so that it
            // behaves like toy duck
            IToyDuck _birdAdapter = new BirdAdapter(_sparrow);

            Console.WriteLine("Sparrow...");
            _sparrow.Fly();
            _sparrow.MakeSound();
            Console.WriteLine("Toy Duck");
            _toyDuck.Squeak();
            // bird behaving like a toy duck
            Console.WriteLine("BirdAdapter...");
            _birdAdapter.Squeak();
            Console.WriteLine();
        }