具有不同返回类别的策略模式实现
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只是一个建议。
您是否考虑过创建一个基结果类并从中派生所有不同的结果类型?这样做你可以考虑使用多态性将结果重新解释为具体类型。
但由于我不深入了解您的设计,这可能会给您增加一些额外的复杂性。至少希望能给大家一些启发:)
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 :)
Switch 也可以替换为 ChainOfResonsibility
Switch also could be replaced with ChainOfResonsibility
某种工厂模式可能
并返回实现接口的对象
Some kind of factory pattern maybe
And return objects that implements an interface as well