工厂方法还是其他模式?

发布于 2024-09-29 17:40:08 字数 384 浏览 2 评论 0原文

我有一个任务实体,需要根据任务类型进行解析。我会将特定类型任务的逻辑封装在一个类中,但是将类型与实现解析逻辑的类相匹配的普遍接受的方法是什么?

我的第一个冲动是做一个工厂,例如:

TaskResolverFactory.GetForType(TaskType) // returns IsATaskResolver, which has a Resolve method

可能在工厂内部,一个案例声明或其他东西。

另一个想法是使用像 StructureMap 这样的东西,但我认为这对于这种情况来说是多余的 - 你同意吗?

我还缺少哪些其他方法,以及替换大型 Case/Switch 语句的普遍接受的方法是什么?

I have a Task entity that needs to be Resolved based on the type of Task it is. I would encapsulate the logic for a specific type of Task in a class, but what is the generally accepted way to match the type with the class that implements the Resolving logic?

My first impulse is to do a Factory, like:

TaskResolverFactory.GetForType(TaskType) // returns IsATaskResolver, which has a Resolve method

Probably inside the Factory, a Case statement or something.

Another thought is to use something like StructureMap, but I think that is overkill for this situation - do you agree?

What other methods am I missing, and what is the generally accepted method for replacing a big Case/Switch statement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不…忘初心 2024-10-06 17:40:08

你是对的 - Factory 是这个问题的经典模式。

如果这是您需要进行此类解析的唯一地方(并且逻辑很简单),我同意 - StructureMap 过度杀伤力。

You are right - a Factory is the classic pattern for this issue.

If this is the only place you need to do such resolution (and the logic is simple), I agree - StructureMap is overkill.

甜宝宝 2024-10-06 17:40:08

你可能会考虑这样的事情:

public class TaskResolverAttribute : Attribute
{
    public Type ResolverType { get; private set; }

    public TaskResolverAttribute(Type resolverType)
    {
        if (!typeof(ITaskResolver).IsAssignableFrom(resolverType))
            throw new ArgumentException("resolverType must implement ITaskResolver");

        ResolverType = resolverType;
    }
}

public class MyTaskResolver : ITaskResolver
{
}

[TaskResolver(typeof(MyTaskResolver))]
public class MyTask
{
}

public static class TaskResolverFactory
{
    public static ITaskResolver GetForType(Type taskType)
    {
        var attribute =
            Attribute.GetCustomAttribute(taskType, typeof(TaskResolverAttribute)) as TaskResolverAttribute;
        if (attribute == null)
            throw new ArgumentException("Task does not have an associated TaskResolver");

        return (ITaskResolver)Activator.CreateInstance(attribute.ResolverType);
    }
}

You might consider something like this:

public class TaskResolverAttribute : Attribute
{
    public Type ResolverType { get; private set; }

    public TaskResolverAttribute(Type resolverType)
    {
        if (!typeof(ITaskResolver).IsAssignableFrom(resolverType))
            throw new ArgumentException("resolverType must implement ITaskResolver");

        ResolverType = resolverType;
    }
}

public class MyTaskResolver : ITaskResolver
{
}

[TaskResolver(typeof(MyTaskResolver))]
public class MyTask
{
}

public static class TaskResolverFactory
{
    public static ITaskResolver GetForType(Type taskType)
    {
        var attribute =
            Attribute.GetCustomAttribute(taskType, typeof(TaskResolverAttribute)) as TaskResolverAttribute;
        if (attribute == null)
            throw new ArgumentException("Task does not have an associated TaskResolver");

        return (ITaskResolver)Activator.CreateInstance(attribute.ResolverType);
    }
}
吾性傲以野 2024-10-06 17:40:08

工厂是一条出路。

至于大 switch 语句,您的其他选项包括:

  • 配置/查找文件
  • 数据库
  • 反射

我认为反射路由是最容易维护的,但是当您还没有类型时(假设您的“类型”是类型)。

既然你已经有了类型,剩下的就很简单了:

public static Task GetByType(Type taskType)
{
    return Activator.CreateInstance(taskType) as Task;
}

A factory is the way to go.

As for the big switch statement, your other options include:

  • Configuration/lookup file
  • A database
  • Reflection

I think the reflection route is the easiest to maintain but the complexity arises when you don't already have the type (assuming your "type" is a Type).

Since you already have the type, the rest is real easy:

public static Task GetByType(Type taskType)
{
    return Activator.CreateInstance(taskType) as Task;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文