/// <summary> /// 非参数化工厂方法(工厂模式) /// </summary> static void FactoryMethod() { //创建工厂 IMouseFactory logitechFactory = new LogitechMouseFactory(); IMouseFactory razeFactory = new RazeMouseFactory(); //通过工厂生产实体 IMouse mouseA = logitechFactory.CreateMouse(); IMouse mouseB = razeFactory.CreateMouse(); Console.WriteLine($"非参数化工厂方法(工厂模式){Environment.NewLine}-------------------------------------------"); Console.WriteLine($"MouseA的品牌是:{mouseA.GetBrand()}"); Console.WriteLine($"MouseB的品牌是:{mouseB.GetBrand()}"); Console.ReadKey(); }
/// <summary> /// 抽象工厂+简单工厂 /// </summary> static void AbstractFactoryWithSimpleFactory() { //创建抽象工厂 IFactory factory = new CommonFactory(); //IFactory factory = new RazeFactory(); //通过工厂生产产品实体 IMouse mouse = factory.CreateMouse(); IKeyBoard keyboard = factory.CreateKeyboard(); Console.WriteLine($"抽象工厂+简单工厂{Environment.NewLine}-------------------------------------------"); Console.WriteLine($"当前工厂生产的鼠标是:{mouse.GetBrand()}"); Console.WriteLine($"当前工厂生产的键盘是:{keyboard.GetBrand()}"); Console.ReadKey(); }
/// <summary> /// 参数化工厂方法(简单工厂) /// </summary> static void SimpleFactory() { //创建工厂 MouseFactory factory = new MouseFactory(); //通过工厂生产实体 //IMouse mouseA = factory.CreateMouse("罗技"); //IMouse mouseB = factory.CreateMouse("雷蛇"); IMouse mouseA = factory.CreateMouse(MouseBrandConst.Logitech); IMouse mouseB = factory.CreateMouse(MouseBrandConst.Raze); Console.WriteLine($"参数化工厂方法(简单工厂){Environment.NewLine}-------------------------------------------"); Console.WriteLine($"MouseA的品牌是:{mouseA.GetBrand()}"); Console.WriteLine($"MouseB的品牌是:{mouseB.GetBrand()}"); Console.ReadKey(); }