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 { } }
static void UsePhone(object obj) { PhoneInterface phone = (PhoneInterface)obj; phone.MakeCall(); if (obj.GetType() == typeof(PhoneBooth)) { PhoneBooth booth = (PhoneBooth)obj; booth.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(); } }