public static void Start() { Console.WriteLine("\nMultipleDispatchWithDoubleDispatch\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: IObjectInSpace and the SpaceShip are tightly coupled. Both need to know about each other. // If we want to introduce a different SpaceShip (e.g. a bigger one that can handle high gravity // better, we would need to add methods to all IObjectInSpace classes. This violates the open/close // principle and it gets worse the more IObjectInSpace we have. }
public string DescribeInteractionWithSpaceShip(SpaceShip ship) { // We know we have a SpaceShip and we know this is a Planet if (Gravity > 3) { return("due to high gravity, the " + ship.Name + " crashes into a planet"); } else { return("the " + ship.Name + " lands safely on this wonderful planet"); } }
public string DescribeInteractionWithSpaceShip(SpaceShip ship) { // We know we have a SpaceShip and we know this is a Planet if (Gravity > 3) { return "due to high gravity, the " + ship.Name + " crashes into a planet"; } else { return "the " + ship.Name + " lands safely on this wonderful planet"; } }
public string DescribeInteractionWithSpaceShip(SpaceShip ship) { // We know we have a SpaceShip and we know this is an Asteroid return("the " + ship.Name + " mines " + Metal + " tons of metal from an asteroid"); }
public static void Start() { Console.WriteLine("\nMultipleDispatchWithDoubleDispatch\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: IObjectInSpace and the SpaceShip are tightly coupled. Both need to know about each other. // If we want to introduce a different SpaceShip (e.g. a bigger one that can handle high gravity // better, we would need to add methods to all IObjectInSpace classes. This violates the open/close // principle and it gets worse the more IObjectInSpace we have. }
public string DescribeInteractionWithSpaceShip(SpaceShip ship) { // We know we have a SpaceShip and we know this is an Asteroid return "the " + ship.Name + " mines " + Metal + " tons of metal from an asteroid"; }