public void Execute() { Console.WriteLine("请选择Iphone:1.Iphone5 2.Iphone6"); string str = Console.ReadLine(); IphonePrice ip = IphoneStoreFactory.CreateIphonePrice(str == "1" ? "Iphone5" : "Iphone6"); ip.Price(); }
internal static IphonePrice CreateIphonePrice(string type) { IphonePrice ip = null; switch (type) { case "Iphone5": ip = new Iphone5Price(); break; case "Iphone6": ip = new Iphone6Price(); break; default: break; } if (ip == null) { throw new Exception(); } return(ip); }