//客户端代码 大量重复代码 static void Main(string[] args) { LeiFeng studentA = SimpleFactory.CreateLeiFeng("大学生"); studentA.BuyRice(); LeiFeng studentB = SimpleFactory.CreateLeiFeng("大学生"); studentB.Sweep(); LeiFeng volunteerB = SimpleFactory.CreateLeiFeng("志愿者"); volunteerB.Wash(); }
public static LeiFeng CreateLeiFeng(string type) { LeiFeng result = null; switch (type) { case "大学生": result = new Undergraduate(); break; case "志愿者": result = new Volunteer(); break; default: break; } return(result); }