static void UsePhone(PhoneInterface phone) { phone.MakeCall(); phone.HangUp(); //calls OpenDoor if a PhoneBooth try { PhoneBooth tester = (PhoneBooth)phone; tester.OpenDoor(); } catch { } //calls TimeTravel if Tardis try { Tardis tester = (Tardis)phone; tester.TimeTravel(); } catch { } }
//Method: UsePhone //Purpose: call MakeCall() and HangUp() using interface, then calls approprate method based on object type //Restrictions: None static void UsePhone(object obj) { PhoneInterface phoneInterface = (PhoneInterface)obj; phoneInterface.MakeCall(); phoneInterface.HangUp(); if (obj.GetType() == typeof(PhoneBooth)) { PhoneBooth phoneBooth = (PhoneBooth)obj; phoneBooth.OpenDoor(); } else if (obj.GetType() == typeof(Tardis)) { Tardis tardis = (Tardis)obj; tardis.TimeTravel(); } }
static void UsePhone(object obj) { PhoneInterface MyInterface = (PhoneInterface)obj; MyInterface.MakeCall(); MyInterface.HangUp(); if (obj.Equals(typeof(PhoneBooth))) { PhoneBooth myBooth = (PhoneBooth)obj; myBooth.openDoor(); } if (obj.Equals(typeof(Tardis))) { Tardis mytardis = (Tardis)obj; mytardis.TimeTravel(); } }