public static void Start() { Console.WriteLine("\nMultipleDispatchWithSwitchCase\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: Whenever a new IObjectInSpace gets introduced a new case statement must be added to the SpaceShip // class. This violates the open/close principle and it gets worse if we have differnt types of SpaceShips }
public static void Start() { Console.WriteLine("\nMultipleDispatchWithSwitchCase\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: Whenever a new IObjectInSpace gets introduced a new case statement must be added to the SpaceShip // class. This violates the open/close principle and it gets worse if we have differnt types of SpaceShips }