C#:开关与工厂类中的方法过载
本文关键字:方法 开关 工厂 | 更新日期: 2023-09-27 18:20:44
给定类:
enum ThingEnum { A,B,C}
interface IThing { }
class A : IThing { }
class B : IThing { }
class C: IThing { }
我脑子里有两个IThingFactory
的实现。一个使用switch
:
class ThingFactory
{
public IThing MakeThing(ThingEnum type)
{
switch (type)
{
case ThingEnum.A:
return new A();
break;
case ThingEnum.B:
return new B();
break;
case ThingEnum.C:
return new C();
break;
default:
break;
}
}
}
另一种使用抽象和方法过载的方法:
class ThingFactory
{
public IThing Make(A a)
{
return new A();
}
public IThing Make(B a)
{
return new B();
}
public IThing Make(C a)
{
return new C();
}
}
我的问题是:
- 哪个实现更快
- 其更可读/更易于理解
- 你会使用哪一种,为什么
我真的建议将其作为您方法的IoC容器。无论如何,也许你只是有一些类,你想在其中确保.ctor
之后类发生了一些事情,这种方法会起作用,而且你不必使用开关。
class IThingFactory
{
public IThing MakeThing<T>() where T : IThing, new()
{
var thing = new T();
thing.Init(); // has to be part of the IThing interface.
return thing;
}
}
一种更通用的方法是
class IThingFactory
{
private IDictionary<Type, Func<IThing>> factories = new Dictionary<Type, Func<IThing>>();
public void Register(Type t, Func<IThing> factory);
{
if(!typeof(IThing).IsAssignableFrom(t))
throw new ArgumentException("This is not a thing");
this.factories.Add(t, factory);
}
public void Register<T>() where T : IThing, new()
{
this.Register<T>(() => new T());
}
public void Register<T>(Func<IThing> factory) where T : IThing
{
this.Register(typeof(T), factory);
}
public IThing MakeThing(Type type);
{
if (!factories.ContainsKey(type))
throw new ArgumentException("I don't know this thing");
return factories[type]();
}
}
public void Main()
{
var factory = new IThingFactory();
factory.Register(typeof(A), () => new A());
factory.Register<B>();
factory.Register<C>(() => new C("Test"));
var instance = factory.MakeThing(typeof(A));
}
使用反射更易于维护。
-
永远不要使用
switch
,因为在添加新类时,还必须修改此switch语句。 -
第二种方法是不可接受的。a、b、c来自哪里?你必须在没有工厂方法的情况下
new
它们。
还要检查一些IoC容器。
您可以使用这样的东西:
internal static class Factory
{
internal static Dictionary<ThingEnum, Func<IThing>> ctors = new Dictionary<ThingEnum, Func<IThing>>
{
{ThingEnum.A, () => new A() },
{ThingEnum.B, () => new B() },
{ThingEnum.C, () => new C() }
};
internal static IThing MakeThing(ThingEnum type)
{
return ctors[type]();
}
}
它比第一种变体更简洁。性能将几乎相同。