public static Nightingale CreateNightingale(string type) { Nightingale result = null; switch (type) { case "學南丁格爾的大學生": result = new Undergraduate(); break; case "社區志願者": result = new Volunteer(); break; } return(result); }
static void Main(string[] args) { // //基本方式:胡鞏代表大學生學習南丁格爾 Nightingale hugong = new Undergraduate(); hugong.BuyRice(); hugong.Sweep(); hugong.Wash(); Nightingale student1 = new Undergraduate(); student1.BuyRice(); Nightingale student2 = new Undergraduate(); student2.Sweep(); Nightingale student3 = new Undergraduate(); student3.Wash(); //簡單工廠模式 Nightingale studentA = SimpleFactory.CreateNightingale("學南丁格爾的大學生"); studentA.BuyRice(); Nightingale studentB = SimpleFactory.CreateNightingale("學南丁格爾的大學生"); studentB.Sweep(); Nightingale studentC = SimpleFactory.CreateNightingale("學南丁格爾的大學生"); studentC.Wash(); //工廠方法模式 IFactory factory = new UndergraduateFactory(); Nightingale student = factory.CreateNightingale(); student.BuyRice(); student.Sweep(); student.Wash(); Console.Read(); }