using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 工厂模式 { class Program { public interface IFruit { } public class Orange : IFruit { public Orange() { Console.WriteLine("An orange is got!"); } } public class Apple : IFruit { public Apple() { Console.WriteLine("An apple is got!"); } } public class FruitFactory { public Orange MakeOrange() { return new Orange(); } public Apple MakeApple() { return new Apple(); } } static void Main(string[] args) { string FruitName = Console.ReadLine(); IFruit MyFruit = null; FruitFactory MyFruitFactory = new FruitFactory(); switch (FruitName) { case "Orange": MyFruit = MyFruitFactory.MakeOrange(); break; case "Apple": MyFruit = MyFruitFactory.MakeApple(); break; default: break; } Console.ReadLine(); } } }