在这种情况下我可以使用工厂模式吗?
我想知道我是否可以 - 以及如何 - 在这种情况下可以使用工厂模式?
我有以下类.....
public interface IStub<T> where T : class
{
IEnumerable<T> CreateStubs();
}
public FooStub : IStub<Foo>
{
public IEnumerable<Foo> CreateStubs() { ... }
}
public BarStub : IStub<Bar>
{
public IEnumerable<Bar> CreateStubs() { ... }
}
等等...
我想知道是否可以通过像这样的工厂方法创建实例...
// This ends up returning an enumerable of Stubs.
var stubs = StubsFactory.CreateStubs<Foo>();
这可能/我在正确的轨道上吗?
I was wondering if I could - and how - I could use the Factory Pattern in this scenario?
I have a the following classes...
public interface IStub<T> where T : class
{
IEnumerable<T> CreateStubs();
}
public FooStub : IStub<Foo>
{
public IEnumerable<Foo> CreateStubs() { ... }
}
public BarStub : IStub<Bar>
{
public IEnumerable<Bar> CreateStubs() { ... }
}
.. etc ...
and I was wondering if it's possible to create the instances through a factory method like ...
// This ends up returning an enumerable of Stubs.
var stubs = StubsFactory.CreateStubs<Foo>();
Is this possible / am I on the right track, here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样。如果实现和存根不在同一程序集或命名空间中,请修改 Type.GetType 行。
替代方案:
用法:
Like this. Modify the Type.GetType line if the implementation and stub isnt in the same assembly or namespace.
Alternative:
Usage:
扩展 jgauffin 关于搜索目标类型的答案:
Extending jgauffin's answer about searching a target type:
工厂声明:
用途:
这是您要找的吗?
Factory declaration:
Usage:
Is it what you're looking for?