考虑一下。我想创建一个创建动物的工厂(模式,而不是计划新的起源)。我想我会很聪明,创建一个类,其中包含我需要的 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/