コード例 #1
0
        public static void Start()
        {
            Console.WriteLine("\nMultipleDispatchWithDynamic\n");

            SpaceShip ship = new SpaceShip()
            {
                Name = "enterprise"
            };
            IObjectInSpace a = new Planet()
            {
                Gravity = 1
            };
            IObjectInSpace b = new Planet()
            {
                Gravity = 5
            };
            IObjectInSpace c = new Asteroid()
            {
                Metal = 3
            };
            IObjectInSpace d = new Asteroid()
            {
                Metal = 2
            };

            Console.WriteLine(ship.DescribeInteraction(a));
            Console.WriteLine(ship.DescribeInteraction(b));
            Console.WriteLine(ship.DescribeInteraction(c));
            Console.WriteLine(ship.DescribeInteraction(d));

            // Downside: This would result in a StackOverflow because there is no method for UnknownObject
            // Console.WriteLine(ship.DescribeInteraction(new UnknownObject()));
        }
コード例 #2
0
        public static void Start()
        {
            Console.WriteLine("\nMultipleDispatchWithDynamic\n");

            SpaceShip ship = new SpaceShip() { Name = "enterprise" } ;
            IObjectInSpace a = new Planet() { Gravity = 1 };
            IObjectInSpace b = new Planet() { Gravity = 5 };
            IObjectInSpace c = new Asteroid() { Metal = 3 };
            IObjectInSpace d = new Asteroid() { Metal = 2 };

            Console.WriteLine(ship.DescribeInteraction(a));
            Console.WriteLine(ship.DescribeInteraction(b));
            Console.WriteLine(ship.DescribeInteraction(c));
            Console.WriteLine(ship.DescribeInteraction(d));

            // Downside: This would result in a StackOverflow because there is no method for UnknownObject
            // Console.WriteLine(ship.DescribeInteraction(new UnknownObject()));
        }