gpt4 book ai didi

c# - 使用 Is/As 运算符的工厂方法

转载 作者:行者123 更新时间:2023-11-30 14:42:39 28 4
gpt4 key购买 nike

我有一个类似于以下代码片段的工厂。 Foo 是 Bar 的包装类,在大多数情况下(但不是全部),存在 1:1 映射。通常,Bar 不知道关于 Foo 的任何信息,但 Foo 获取了 Bar 的一个实例。是否有更好/更清洁的方法来执行此操作?

public Foo Make( Bar obj )
{
if( obj is Bar1 )
return new Foo1( obj as Bar1 );
if( obj is Bar2 )
return new Foo2( obj as Bar2 );
if( obj is Bar3 )
return new Foo3( obj as Bar3 );
if( obj is Bar4 )
return new Foo3( obj as Bar4 ); // same wrapper as Bar3
throw new ArgumentException();
}

乍一看,这个问题可能看起来像是重复的(也许是),但我还没有看到完全相同的问题。这是一个接近但不完全是的:

Factory based on Typeof or is a

最佳答案

如果这些是引用类型,那么在 is 之后调用 as 是不必要的开销。通常的习惯用法是使用 as 进行转换并检查是否为 null。

从微优化退后一步,看起来您可以使用您链接到的文章中的一些技术。具体来说,您可以创建一个以类型为键的字典,其值是构造实例的委托(delegate)。代表将接受 Bar 的(子)并返回 Foo 的(子)。理想情况下,Foo 的每个 child 都会将自己注册到字典中,字典在 Foo 本身中可以是静态的。

下面是一些示例代码:

// Foo creator delegate.
public delegate Foo CreateFoo(Bar bar);

// Lookup of creators, for each type of Bar.
public static Dictionary<Type, CreateFoo> Factory = new Dictionary<Type, CreateFoo>();

// Registration.
Factory.Add(typeof(Bar1), (b => new Foo1(b)));

// Factory method.
static Foo Create(Bar bar)
{
CreateFoo cf;
if (!Factory.TryGetValue(bar.GetType(), out cf))
return null;

return cf(bar);
}

关于c# - 使用 Is/As 运算符的工厂方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2959303/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com