我开始研究不同的设计模式,现在我专注于工厂设计模式.我看了一些例子,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.
因此,界面似乎是工厂设计模式的重要组成部分,但是我发现在主方法中创建集合时实际的唯一原因.如果你不想要它,你可以删除它(看下面的代码,可能的地方),它仍然像计划的那样工作.
- using System;
- using System.Collections.Generic;
- using System.Collections;
- namespace FactoryDesignPattern
- {
- class Program
- {
- static void Main(string[] args)
- {
- var FordFiestaFactory = new FordFiestaFactory();
- var FordFiesta = FordFiestaFactory.CreateCar("Blue");
- Console.WriteLine("Brand: {0} \nModel: {1} \nColor: {2}",FordFiesta.Make,FordFiesta.Model,FordFiesta.Color);
- Console.WriteLine();
- //Inserted this later. Using a collection requires the Interface to be there.
- List<ICreateCars> Cars = new List<ICreateCars>();
- Cars.Add(new FordFiestaFactory());
- Cars.Add(new BMWX5Factory());
- foreach (var Car in Cars)
- {
- var ProductCar = Car.CreateCar("Red");
- Console.WriteLine("Brand: {0} \nModel: {1} \nColor: {2}",ProductCar.Make,ProductCar.Model,ProductCar.Color);
- Console.WriteLine();
- }
- Console.ReadKey();
- }
- }
- public abstract class Car
- {
- public string Make { get; set; }
- public string Model { get; set; }
- public string EngineSize { get; set; }
- public string Color { get; set; }
- }
- public class FordFiesta : Car
- {
- public FordFiesta()
- {
- Make = "Ford";
- Model = "Fiesta";
- EngineSize = "1.1";
- }
- }
- public class BMWX5 : Car
- {
- public BMWX5()
- {
- Make = "BMW";
- Model = "X5";
- EngineSize = "2.1";
- }
- }
- public interface ICreateCars
- {
- Car CreateCar(string color);
- }
- class FordFiestaFactory : ICreateCars
- {
- public Car CreateCar(string color)
- {
- return new FordFiesta() { Color = color };
- }
- }
- class BMWX5Factory : ICreateCars
- {
- public Car CreateCar(string color)
- {
- return new BMWX5(){ Color = color };
- }
- }
- }
那我为什么需要那个接口呢?我阅读了多个抽象的表达式,但我没有得到它,所以我更喜欢实际的答案.
提前致谢!
解决方法
只要工厂的调用者不知道工厂的类型,接口(或抽象工厂基类,它实际上与有效的接口相同)就很有用.
您为自己的实际示例提供了基础,因此我将在此处添加我的解释,为什么在获得工厂列表时这不仅有用:
想象一下这种方法应该在适当时创建汽车,而不知道要创建什么类型的汽车(由工厂实施决定).该方法查看Person对象,该对象具有OwnsCar属性,该属性最终决定是否应该调用factory方法:
- public Car CreateCarForPerson(Person person,ICreateCars carType)
- {
- if (person.OwnsCar) {
- return carType.CreateCar("red");
- } else {
- return null;
- }
- }
以同样的方式,您也可以使用这样的工厂来创建任意数量的汽车:
- public Car[] CreateAnyNumberOfCars(ICreateCars carType)
- {
- var result = new List<Car>();
- for (int i = new Random().Next(100); i >= 0; i--) {
- result.Add(carType.CreateCar("blue"));
- }
- return result.ToArray();
- }
注意这两种方法都不知道正在创建什么样的车型;他们使用的工厂只知道界面,但不是确切的类型.
因此,如果您希望能够提供不同的工厂实现,则可以为工厂声明一个通用接口.如果您的工厂仅用于使您的调用者远离目标构造函数的直接调用,则不需要工厂接口.