static void Main(string[] args) { IFactory factory = new UndergraduateFactory();// 只需要更换此处的代码即可 // IFactory factory = new VolunteerFacroey(); LeiFeng student = factory.CreateLeiFeng(); student.BuyRice(); student.Sweep(); student.Wash(); }
public static LeiFeng CreateLeiFeng(string type) { LeiFeng result = null; switch (type) { case "学雷锋的大学生": result = new LeiFeng(); break; case "志愿者": result = new Volunteer(); break; default: break; } return(result); }