c# – 工厂设计模式 – 为什么需要接口?

前端之家收集整理的这篇文章主要介绍了c# – 工厂设计模式 – 为什么需要接口?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我开始研究不同的设计模式,现在我专注于工厂设计模式.我看了一些例子,youtube tuturials和博客,我得到了最多,但我仍然没有得到为什么接口是必要的.

官方定义是:

Define an interface for creating an object,but let subclasses decide
which class to instantiate. Factory Method lets a class defer
instantiation to subclasses.

因此,界面似乎是工厂设计模式的重要组成部分,但是我发现在主方法中创建集合时实际的唯一原因.如果你不想要它,你可以删除它(看下面的代码,可能的地方),它仍然像计划的那样工作.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4.  
  5. namespace FactoryDesignPattern
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. var FordFiestaFactory = new FordFiestaFactory();
  12. var FordFiesta = FordFiestaFactory.CreateCar("Blue");
  13. Console.WriteLine("Brand: {0} \nModel: {1} \nColor: {2}",FordFiesta.Make,FordFiesta.Model,FordFiesta.Color);
  14. Console.WriteLine();
  15.  
  16.  
  17. //Inserted this later. Using a collection requires the Interface to be there.
  18. List<ICreateCars> Cars = new List<ICreateCars>();
  19. Cars.Add(new FordFiestaFactory());
  20. Cars.Add(new BMWX5Factory());
  21.  
  22. foreach (var Car in Cars)
  23. {
  24. var ProductCar = Car.CreateCar("Red");
  25. Console.WriteLine("Brand: {0} \nModel: {1} \nColor: {2}",ProductCar.Make,ProductCar.Model,ProductCar.Color);
  26. Console.WriteLine();
  27. }
  28.  
  29. Console.ReadKey();
  30. }
  31. }
  32.  
  33. public abstract class Car
  34. {
  35. public string Make { get; set; }
  36. public string Model { get; set; }
  37. public string EngineSize { get; set; }
  38. public string Color { get; set; }
  39. }
  40.  
  41. public class FordFiesta : Car
  42. {
  43. public FordFiesta()
  44. {
  45. Make = "Ford";
  46. Model = "Fiesta";
  47. EngineSize = "1.1";
  48. }
  49. }
  50.  
  51. public class BMWX5 : Car
  52. {
  53. public BMWX5()
  54. {
  55. Make = "BMW";
  56. Model = "X5";
  57. EngineSize = "2.1";
  58. }
  59. }
  60.  
  61. public interface ICreateCars
  62. {
  63. Car CreateCar(string color);
  64. }
  65.  
  66. class FordFiestaFactory : ICreateCars
  67. {
  68. public Car CreateCar(string color)
  69. {
  70. return new FordFiesta() { Color = color };
  71. }
  72. }
  73.  
  74. class BMWX5Factory : ICreateCars
  75. {
  76. public Car CreateCar(string color)
  77. {
  78. return new BMWX5(){ Color = color };
  79. }
  80. }
  81. }

那我为什么需要那个接口呢?我阅读了多个抽象的表达式,但我没有得到它,所以我更喜欢实际的答案.

提前致谢!

解决方法

只要工厂的调用者不知道工厂的类型,接口(或抽象工厂基类,它实际上与有效的接口相同)就很有用.

您为自己的实际示例提供了基础,因此我将在此处添加我的解释,为什么在获得工厂列表时这不仅有用:

想象一下这种方法应该在适当时创建汽车,而不知道要创建什么类型的汽车(由工厂实施决定).该方法查看Person对象,该对象具有OwnsCar属性,该属性最终决定是否应该调用factory方法

  1. public Car CreateCarForPerson(Person person,ICreateCars carType)
  2. {
  3. if (person.OwnsCar) {
  4. return carType.CreateCar("red");
  5. } else {
  6. return null;
  7. }
  8. }

以同样的方式,您也可以使用这样的工厂来创建任意数量的汽车:

  1. public Car[] CreateAnyNumberOfCars(ICreateCars carType)
  2. {
  3. var result = new List<Car>();
  4. for (int i = new Random().Next(100); i >= 0; i--) {
  5. result.Add(carType.CreateCar("blue"));
  6. }
  7. return result.ToArray();
  8. }

注意这两种方法都不知道正在创建什么样的车型;他们使用的工厂只知道界面,但不是确切的类型.

因此,如果您希望能够提供不同的工厂实现,则可以为工厂声明一个通用接口.如果您的工厂仅用于使您的调用者远离目标构造函数的直接调用,则不需要工厂接口.

猜你在找的C#相关文章