Exemplo n.º 1
0
        static void Main(string[] args)
        {
            var dragon = new Dragon();

            Console.WriteLine(dragon.Crawl());
            Console.WriteLine(dragon.Fly());

            dragon.Age = 5;
            Console.WriteLine(dragon.Crawl());
            Console.WriteLine(dragon.Fly());

            dragon.Age = 15;
            Console.WriteLine(dragon.Crawl());
            Console.WriteLine(dragon.Fly());
        }
Exemplo n.º 2
0
        public static void MultipleInheritance()
        {
            var d = new Dragon();

            d.Crawl();
            d.Fly();
        }
        static void Main(string[] args)
        {
            Dragon dg = new Dragon();

            dg.Age = 10;

            Console.WriteLine(dg.Fly());
        }
Exemplo n.º 4
0
        public void TestCase()
        {
            var bird = new Bird {
                Age = 9
            };
            var lizard = new Lizard {
                Age = 9
            };
            var dragon = new Dragon {
                Age = 9
            };

            Assert.AreEqual(dragon.Fly(), bird.Fly());
            Assert.AreEqual(dragon.Crawl(), lizard.Crawl());
        }
Exemplo n.º 5
0
        private static void Main(string[] args)
        {
            /*
             * In this example we have a simple decorator which extends StringBuilder
             * type of .Net and add it the functionality of string concatenation by += operator
             * and implicit conversion from string to it's type.
             */
            var assignableSb = new AssignableStringBuilder();

            assignableSb  = "Hello";
            assignableSb += " World";
            WriteLine(assignableSb);

            /*
             * Decorator pattern is used to add or extend functionality of an
             * existing type. In this example we decorate the basic User class
             * to have a last name when it becomes a VipUser.
             */

            var user    = new User("John", 24);
            var vipUser = new VipUser(user, "Doe");

            WriteLine(user);
            WriteLine(vipUser);

            /*
             * Demonstration of C# extension methods to apply decorator pattern.
             */
            var user2   = new User("Jane", 17);
            var ageInfo = user2.IsAdult() ? "an adult" : "not an adult";

            WriteLine($"{user2} is {ageInfo}");

            var vipUser2 = user2.ConvertToVip("Doe");

            WriteLine($"{vipUser2} is a VIP user now.");

            /*
             * Course exercise
             */
            var dragon = new Dragon()
            {
                Age = 0
            };

            WriteLine(dragon.Fly());
            WriteLine(dragon.Crawl());
        }