具有不同返回类别的策略模式实现

发布于 2024-08-16 07:28:09 字数 338 浏览 2 评论 0原文

我有一个 MessageProcessor 类,它处理不同类型的 xml 消息。基于消息类型的 switch 语句 (C#) 调用适当的方法来解析 xml 并提取消息类型所需的数据。

我宁愿有许多解析器类,其中之一将在根据消息类型创建 MessageProcessor 时注入到 MessageProcessor 中。用多态性替换 Switch - 到目前为止一切顺利。

但是,我遇到的问题是当前解析器方法每个返回不同的结果,例如 ParseExecute(xml, out Session)、ParseCallback(xml, out id, out name, ...)

是否可以做我想做的事情在这种情况下?

I have a MessageProcessor class which processes xml messages of different types. A switch statement (C#) based on the message type calls the appropriate method to parse the xml and extract the data required for the type of message.

I would rather have a number of parser classes, one of which will be injected into the MessageProcessor when it is created based on the message type. Switch replaced with polymorphism - so far so good.

However, the problem I have is that the current parser methods each return different results, e.g. ParseExecute(xml, out Session), ParseCallback(xml, out id, out name, ...)

Is it possible to do what I want to do in this scenario?

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

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

发布评论

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

评论(3

逆蝶 2024-08-23 07:28:09

只是一个建议。

您是否考虑过创建一个基结果类并从中派生所有不同的结果类型?这样做你可以考虑使用多态性将结果重新解释为具体类型。

但由于我不深入了解您的设计,这可能会给您增加一些额外的复杂性。至少希望能给大家一些启发:)

Just a suggestion.

Had you think about create a base result class and derive all different result types from it? Doing in that way you can think in use polymorphism to re-interpret the result to the concrete type.

But as I don't know your design in depth this can add some extra complexity for you. At least hope it can give some inspiration :)

在梵高的星空下 2024-08-23 07:28:09

Switch 也可以替换为 ChainOfResonsibility

Switch also could be replaced with ChainOfResonsibility

雨夜星沙 2024-08-23 07:28:09

某种工厂模式可能

public class ParserFactory
    {
        public static IParser Create(string type)
        {
            IParser parser;
            switch (type)
            {
                case "1":
                    parser = new Parser1();
                    break;
                case "2":
                    parser = new Parser2();
                    break;
                default:
                    throw new ArgumentOutOfRangeException("type");
            }

            return parser;
        }
    }

并返回实现接口的对象

public class Parser1 : IParser
    {

        public IParseResult Parse(string xml)
        {
            //Set values

            return result;
        }
    }

Some kind of factory pattern maybe

public class ParserFactory
    {
        public static IParser Create(string type)
        {
            IParser parser;
            switch (type)
            {
                case "1":
                    parser = new Parser1();
                    break;
                case "2":
                    parser = new Parser2();
                    break;
                default:
                    throw new ArgumentOutOfRangeException("type");
            }

            return parser;
        }
    }

And return objects that implements an interface as well

public class Parser1 : IParser
    {

        public IParseResult Parse(string xml)
        {
            //Set values

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