【C#设计模式(2)——工厂模式】
前言
工厂模式:使用工厂创建对象。
工厂模式的主要目的是分离对象的创建与调用,通过使用工厂统一管理对象的创建。工厂模式可以隐藏对象的创建细节,使客户终端代码只关注使用对象而不需要关注对象的创建过程。
运行结果
代码
#region 食品
//食品接口
public interface IFood
{void CreateFood(); //创建食品void SetManufacturer(string manufacturer); //设置制造商
}
// 食品类
public class Food
{protected string Name { get; set; }protected string Manufacturer { get; set; }protected string Description { get; set; }
}// 具体罐头食品类:继承食品类,实现食品接口。
public class ConcreteCannedFood : Food, IFood
{public ConcreteCannedFood(string name, string description){Name = name;Description = description;}public void CreateFood(){Console.WriteLine($"【食品名称】:{Name}.");Console.WriteLine($"【生产厂商】:{Manufacturer}.");Console.WriteLine($"【食品描述】:{Description}.");Console.WriteLine();}public void SetManufacturer(string manufacturer){Manufacturer = manufacturer;}
}
#endregion#region 食品工厂
// 工厂接口
public interface IFactory
{IFood ProduceFood(string name); //生产食品
}
// 工厂类
public class Factory
{protected string Name { get; set; }
}
// 罐头食品工厂类
public class CannedFoodFactory : Factory, IFactory
{public CannedFoodFactory(string name){this.Name = name;Console.WriteLine($"【{this.Name}】开始生产...");Console.WriteLine("-----------------------------------------------------");}public IFood ProduceFood(string name){IFood food =null;switch (name){case FoodList.苹果罐头:food = new ConcreteCannedFood(FoodList.苹果罐头, FoodList.苹果罐头描述);break;case FoodList.橙子罐头:food = new ConcreteCannedFood(FoodList.橙子罐头, FoodList.橙子罐头描述);break;case FoodList.黄桃罐头:food = new ConcreteCannedFood(FoodList.黄桃罐头, FoodList.黄桃罐头描述);break;case FoodList.凤梨罐头:food = new ConcreteCannedFood(FoodList.凤梨罐头, FoodList.凤梨罐头描述);break;case FoodList.菠萝罐头:food = new ConcreteCannedFood(FoodList.菠萝罐头, FoodList.菠萝罐头描述);break;case FoodList.草莓罐头:food = new ConcreteCannedFood(FoodList.草莓罐头, FoodList.草莓罐头描述);break;case FoodList.樱桃罐头:food = new ConcreteCannedFood(FoodList.樱桃罐头, FoodList.樱桃罐头描述);break;case FoodList.葡萄罐头:food = new ConcreteCannedFood(FoodList.葡萄罐头, FoodList.葡萄罐头描述);break;case FoodList.橘子罐头:food = new ConcreteCannedFood(FoodList.橘子罐头, FoodList.橘子罐头描述);break;case FoodList.柠檬罐头:food = new ConcreteCannedFood(FoodList.柠檬罐头, FoodList.柠檬罐头描述);break;default:return null;}food.SetManufacturer(this.Name);food.CreateFood();return food;}
}
#endregion#region 清单
/// <summary>
/// 食品清单
/// </summary>
public class FoodList
{public const string 苹果罐头 = "苹果罐头";public const string 橙子罐头 = "橙子罐头";public const string 黄桃罐头 = "黄桃罐头";public const string 凤梨罐头 = "凤梨罐头";public const string 菠萝罐头 = "菠萝罐头";public const string 草莓罐头 = "草莓罐头";public const string 樱桃罐头 = "樱桃罐头";public const string 葡萄罐头 = "葡萄罐头";public const string 橘子罐头 = "橘子罐头";public const string 柠檬罐头 = "柠檬罐头";public const string 苹果罐头描述 = "苹果罐头由苹果、白砂糖、柠檬汁等原材料制成,具有补充营养、提供能量、促进消化等功效";public const string 橙子罐头描述 = "橙子罐头采用新鲜橙子制成,富含维生素C,具有增强免疫力、清新口气的效果。";public const string 黄桃罐头描述 = "黄桃罐头由成熟的桃子制成,口感鲜美,含有丰富的维生素和矿物质,有助于美容养颜。";public const string 凤梨罐头描述 = "凤梨罐头选用新鲜梨子,清甜可口,具有润肺止咳、清热解毒的功效。";public const string 菠萝罐头描述 = "菠萝罐头由新鲜菠萝制成,酸甜可口,含有丰富的维生素C和纤维素,有助于消化。";public const string 草莓罐头描述 = "草莓罐头采用新鲜草莓,色泽鲜艳,味道甜美,富含抗氧化剂,有助于抗衰老。";public const string 樱桃罐头描述 = "樱桃罐头由新鲜樱桃制成,口感酸甜,富含维生素和矿物质,有助于改善睡眠。";public const string 葡萄罐头描述 = "葡萄罐头选用新鲜葡萄,味道鲜美,含有丰富的抗氧化剂,有助于保护心脏健康。";public const string 橘子罐头描述 = "橘子罐头由新鲜橘子制成,口感酸甜,富含维生素C,具有促进消化、增强免疫力的功效。";public const string 柠檬罐头描述 = "柠檬罐头采用新鲜柠檬制成,酸味浓郁,富含维生素C,有助于清新口气和美容养颜。";
}
/// <summary>
/// 工厂清单
/// </summary>
public class FactoryList
{public const string 温州罐头厂 = "温州罐头厂";public const string 广州罐头厂 = "广州罐头厂";public const string 北京罐头厂 = "北京罐头厂";public const string 上海罐头厂 = "上海罐头厂";public const string 深圳罐头厂 = "深圳罐头厂";
}/** 工厂模式:使用工厂创建对象*/internal class Program{static void Main(string[] args){IFactory factory1 = new CannedFoodFactory(FactoryList.北京罐头厂);IFood food1 = factory1.ProduceFood(FoodList.苹果罐头);IFood food2 = factory1.ProduceFood(FoodList.黄桃罐头);IFood food3 = factory1.ProduceFood(FoodList.凤梨罐头);IFood food4 = factory1.ProduceFood(FoodList.柠檬罐头);IFactory factory2 = new CannedFoodFactory(FactoryList.温州罐头厂);IFood food11 = factory2.ProduceFood(FoodList.苹果罐头);IFood food12 = factory2.ProduceFood(FoodList.黄桃罐头);IFood food13 = factory2.ProduceFood(FoodList.草莓罐头);IFood food14 = factory2.ProduceFood(FoodList.菠萝罐头);Console.ReadLine();Console.ReadLine();}}
#endregion