public static Nightingale Create(string type) { Nightingale result = null; switch (type) { case "Student": result = new Undergraduate(); break; case "Volunteer": result = new Volunteer(); break; default: break; } return(result); }
// Factory Pattern static void Main(string[] args) { Console.WriteLine("Simple Factory Pattern"); Nightingale stA = SimpleFactory.Create("Student"); stA.BuyRice(); Nightingale stB = SimpleFactory.Create("Student"); stB.Sweep(); Nightingale stC = SimpleFactory.Create("Student"); stC.Wash(); Console.Read(); Console.WriteLine("Factory Pattern"); IFactory factory = new UndergraduateFactory(); Nightingale stD = factory.Create(); stD.BuyRice(); stD.Sweep(); stD.Wash(); Console.Read(); Console.Read(); }