static void Main(string[] args) { Fruit fruit = FruitFactory.GetFruit("Apple"); fruit.MethodSame(); fruit.MethodDiff(); }
static void Main(string[] args) { //设计模式: 简单工厂 //功能:根据提供字符串不同,得到某一个接口的特定实例,并进行一些自动化处理 //*这个模式不属于GoF23中的设计模式 IFruit fruit = FruitFactory.CreateFruit("Apple"); IFruit fruit2 = FruitFactory.CreateFruit("Banana"); Console.WriteLine($"fruit的信息是:{fruit.Info()}"); Console.WriteLine($"fruit2的信息是:{fruit2.Info()}"); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("简单工厂模式"); var fruitFactory = new FruitFactory(); var apple = fruitFactory.Create("Apple"); apple.Eat(); var pear = fruitFactory.Create("Pear"); pear.Eat(); Console.ReadKey(); }
static void Main(string[] args) { foreach (FruitType type in Enum.GetValues(typeof(FruitType)))//遍历枚举 { var product = FruitFactory.CreateFruit(type); System.Console.WriteLine("现在创建了一个{0},它的类型是{1}", product.GetProductName(), product.GetProductType()); } System.Console.WriteLine("通过配置文件创建对象:"); string target = ConfigurationManager.AppSettings["TargetObject"]; int index; if (int.TryParse(target, out index)) { System.Console.WriteLine("配置项结果是{0}", index.ToString()); var product = FruitFactory.CreateFruit((FruitType)index); System.Console.WriteLine("现在创建了一个{0},它的类型是{1}", product.GetProductName(), product.GetProductType()); } System.Console.ReadKey();//等待按键 }