将对象的创建方法封装在专门的工厂类中。比如中国坦克、美国坦克继承自坦克, 中国飞机、美国飞机继承自飞机,但是在实例化的时候想根据阵营来实例化,这时候创建一个中国武器工厂和美国武器工厂,用工厂类中的方法去返回对象,中国工厂返回中国武器,美国工厂返回美国武器。

工厂模式能很好的帮助我们实现从类爆炸到只需几个关键类

细讲:https://www.youtube.com/watch?v=EcFVTgRHJLM&list=PLrhzvIcii6GNjpARdnO4ueTUAVR9eMBpc&index=5

问题背景

在策略模式等模式中,我们注入对象到其中进行处理,换句话说,策略模式等是依赖注入的。

而工厂模式便是创建/实例化注入对象。

换句话说,当我们不知道实例化什么对象 需要一些逻辑来决定实例化对象的时候,就可以用工厂模式。

例子

1.假设有一个生态系统,里面有三个动物:Dog,Cat,Duck.

开始一段时间,三种动物随机生成,即RandomlyCreate()

当生态系统破坏时,三种动物有规律的生成,即RegularlyCreate()

  • 从上面例子得出,我们创建的对象是相同的,只是创建的逻辑(算法)不同。

2.假设你在开发一个飞机大战的游戏,现在需要根据关卡等级生成许多的陨石,它们有属性:VelocityX,VelocityY,Size等

当关卡越高,陨石相应属性越大。

  • 从上面例子得出,我们创建的对象是相同的,只是创建的子类(或属性)不同。

工厂模式

定义:定义一个创建对象的接口,让其子类自己决定实例化哪一个工厂类,工厂模式使其创建过程延迟到子类进行。

何时使用:我们明确地计划不同条件下创建不同实例时。

如何解决:让其子类实现工厂接口,返回的也是一个抽象的产品。

关键代码:创建过程在其子类执行。

应用实例: 1、您需要一辆汽车,可以直接从工厂里面提货,而不用去管这辆汽车是怎么做出来的,以及这个汽车里面的具体实现。 2、Hibernate 换数据库只需换方言和驱动就可以。

游戏开发中的工厂方法模式 - 图1
回到第一个例子,它的伪UML应该是这样的:

游戏开发中的工厂方法模式 - 图2

右边负责创建 How to creat something

左边 what we want to creat

不同场景下你需要不同的逻辑决定实例化什么 这个逻辑可以封装成工厂

换句话说,工厂负责封装逻辑

客户端不必知道怎么构造 为什么构造或需要传递什么参数来构造这个对象

板子

  1. using System;
  2. using System.Collections.Generic;
  3. namespace FactoryMethodPattern
  4. {
  5. //抽象工厂
  6. interface IAbstractAnimalFactory
  7. {
  8. public List<IAbstractAnimal> CreateAnimals();
  9. }
  10. class RandomFactory : IAbstractAnimalFactory
  11. {
  12. public RandomFactory()
  13. {
  14. Console.WriteLine("随机化工厂实例化。");
  15. }
  16. public List<IAbstractAnimal> CreateAnimals()
  17. {
  18. List<IAbstractAnimal> animal = new List<IAbstractAnimal>();
  19. animal.Add(new Dog());
  20. animal.Add(new Duck());
  21. animal.Add(new Dog());
  22. animal.Add(new Cat());
  23. return animal;
  24. }
  25. }
  26. class RegularlyFactory : IAbstractAnimalFactory
  27. {
  28. int a, b, c;
  29. public RegularlyFactory(int dog,int cat,int duck)
  30. {
  31. Console.WriteLine("规律化工厂实例化。");
  32. a = dog;
  33. b = cat;
  34. c = duck;
  35. }
  36. public List<IAbstractAnimal> CreateAnimals()
  37. {
  38. List<IAbstractAnimal> animal = new List<IAbstractAnimal>();
  39. for (int i = 0; i < a; i++) animal.Add(new Dog());
  40. for (int i = 0; i < b; i++) animal.Add(new Cat());
  41. for (int i = 0; i < c; i++) animal.Add(new Duck());
  42. return animal;
  43. }
  44. }
  45. //抽象产品
  46. interface IAbstractAnimal
  47. {
  48. public string show();
  49. }
  50. //具体产品
  51. class Dog : IAbstractAnimal
  52. {
  53. public Dog()
  54. {
  55. Console.WriteLine("生成一条狗");
  56. }
  57. public string show()
  58. {
  59. return "这是一条狗";
  60. }
  61. }
  62. class Cat : IAbstractAnimal
  63. {
  64. public Cat()
  65. {
  66. Console.WriteLine("生成一只猫。");
  67. }
  68. public string show()
  69. {
  70. return "这是一只猫";
  71. }
  72. }
  73. class Duck : IAbstractAnimal
  74. {
  75. public Duck()
  76. {
  77. Console.WriteLine("生成一只鸭。");
  78. }
  79. public string show()
  80. {
  81. return "这是一只鸭";
  82. }
  83. }
  84. class Program
  85. {
  86. static void Main(string[] args)
  87. {
  88. List<IAbstractAnimal> zoo = new List<IAbstractAnimal>();
  89. IAbstractAnimalFactory factory;
  90. factory = new RandomFactory();
  91. zoo.AddRange(factory.CreateAnimals());
  92. factory = new RegularlyFactory(3,3,3);
  93. zoo.AddRange(factory.CreateAnimals());
  94. for(int i= 0; i < zoo.Count; i++)
  95. {
  96. Console.WriteLine("第" + i + "只动物:" + zoo[i].show());
  97. }
  98. }
  99. }
  100. }

根据描述自己写的 希望没有错🙏

游戏实例——角色工厂类

游戏开发中的工厂方法模式 - 图3From: 《设计模式与游戏完美开发》