c# - 一劳永逸地使用泛型实现工厂模式

标签 c# oop generics inheritance

考虑一下。我想创建一个创建动物的工厂(模式,而不是计划新的起源)。我想我会很聪明,创建一个类,其中包含我需要的 3 样东西,

  • 返回抽象动物的委托(delegate)
  • 为每种动物返回特定动物的创建方法
  • 使用委托(delegate)的每个创建方法的实例

厌倦了再次这样做并且每次我需要使用工厂模式时都会有所收获,我想我会格外聪明并一次解决它。所以,我创建了这个漂亮的类

class Factorable<T> where T: class, new() 
{
    delegate T CreateDelegate();
    static CreateDelegate DoCreate = new CreateDelegate (CreateSelf);
    static T CreateSelf()
    {
        return new T();
    }
}

class Factory<T> where T : Factorable<T>
{
    public Factorable<T>.CreateDelegate CreationMethod ;
} 

我想,太棒了,我可以让顶级类 (Animal) 继承自此类,这样我就不必为所有动物编写和实例化所有特定的创建方法。多亏了泛型,这一切都将完成。几乎...看到这个:

class Animal:Factorable<Animal> {...}
class Bird:Animal {...}

Factory genesis = new Factory<Animal>();
genesis.CreationMethod = Animal.DoCreate;
Animal instance = genesis.CreateAnimal();  //instance is a brand new abstract Animal

genesis.CreationMethod = Bird.DoCreate;  //lets make it create birds!
instance = genesis.CreateAnimal();  // wrong, instance is still an abstract Animal

有什么办法可以解决这个问题吗?我想要 Bird 继承的 CreateSelf 方法来创建 Birds,而不是抽象的 Animals(无需为 Bird 编写新方法)。有没有一种方法可以指定 Animal 继承自 Factorable 但其后代可以用自己的类型覆盖泛型 T?

像这样的东西(这是愚蠢的代码,不起作用)

class Animal:Factorable<Animal... or better the actual type of the class that has inherited>

最佳答案

你是不是有点过于复杂了?假设 Animal是你的基类:

public class Factory
{
    public static T Create<T>() where T : Animal, new()
    {
        return new T();
    }
}

用法:

var a = Factory.Create<Animal>();
var b = Factory.Create<Bird>();

更新

阅读您的评论后,我是这样理解的:调用工厂的对象不知道所创建实例的确切类型。它只知道它是 Animal 或 Animal 派生类。那么,这个怎么样:

public class Factory
{
    private Type _outputType = typeof(Animal);

    public void Produces<T>() where T : Animal, new()
    {
        _outputType = typeof(T);
    }

    public Animal CreateAnimal()
    {
        return (Animal)Activator.CreateInstance(_outputType);
    }
}

注意:将输出类型设为私有(private)并使用 Produces<T>设置它提供了一种简单的方法来确保输出类型是 Animal 或 derived。

用法:

var f = new Factory();  // factory produces animals
var a = f.CreateAnimal();
f.Produces<Bird>();     // from now on factory produces birds
var b = f.CreateAnimal();

关于c# - 一劳永逸地使用泛型实现工厂模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22103768/

相关文章:

c# - 是否可以在 Windows 上将 Mono 脚本引擎与 Cake 一起使用?

java - 类术语 "attribute"vs "member"vs "variable"vs "field"

javascript - 在 Javascript 中将参数传递给 new()

c++ - 在一个类中有 "typedef void FuncCharPtr"如何将使用该 typedef 的函数与非静态函数一起使用?

c# - 与泛型类中的方法不同的返回类型

java - EMF 通用列表

c# - 名称为 'Id' 的成员已存在于 `Model` 上。使用 JsonPropertyAttribute 指定另一个名称

c# - 用于 C#/.NET 的良好 2D 基元渲染库?

c# - .NET 中的 channel 工厂是什么?

java - 将泛型数组传递给采用泛型数组的方法