设计模式:解析文本文件中相似但不同的模式
在此先感谢您的帮助。 我想知道是否有一种(设计)模式可以应用于这个问题。
我希望从具有相似但不同格式的文本文件中解析、处理和提取值。
更具体地说,我正在构建一个处理引擎,它接受来自多个不同格式的在线扑克手牌历史记录文件网站并解析出特定的数据字段(手号、日期时间、玩家)。 对于每种格式,我需要解析文件的逻辑略有不同,但提取值的处理将是相同的。
我的第一个想法是创建一个类,它接受每种文件类型的“模式”并相应地进行解析/处理。 我确信对此有更好的解决方案。
谢谢!
奖励积分: C# 中的任何具体实现提示。
thanks in advance for your help. I am wondering if there is a (design) pattern that can be applied to this problem.
I am looking to parse, process, and extract out values from text files with similar, but differing formats.
More specifically, I am building a processing engine that accepts Online Poker Hand History files from a multitude of different websites and parses out specific data fields (Hand #, DateTime, Players). I will need the logic to parse the files to be slightly different for each format, but the processing of the extracted values will be the same.
My first thought would be to create just 1 class that accepts a "schema" for each file type and parses/processes accordingly. I am sure there is a better solution to this.
Thanks!
Bonus Point:
Any specific implementation hints in C#.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这听起来像是策略模式的候选者。 可以在此处找到一个 C# 示例,以及另一个 此处。 维基百科上提供了简要说明。
更完整的描述可在 Fowler 和 克里耶夫斯基。
也可以从GoF 书中找到它。
This sounds like a candidate for the Strategy pattern. An example in C# can be found here and another one here. A brief description is available on Wikipedia.
More complete descriptions is available in book by Fowler and Kerievsky.
It is also available from the GoF book.
“Provider”模式就是您正在寻找的模式...它是 ADO.Net 中使用的模式。 每个数据库供应商都有一个单独的数据“Provider”,它“知道”如何从其特定的数据库供应商产品中读取数据,但以标准格式(接口)将其传递给下游系统......您将编写一个小型“Provider”组件(一个类就足够了)“知道”每个不同网站扑克历史数据提供者的格式,并以完全相同的方式将该数据公开给读取它的上游系统......
The "Provider" pattern is the one you're looking for... it is what is used in ADO.Net. Each database vendor has a separate data "Provider" that "knows" how to read the data from it's specific DB vendors product, but delivers it in a standard format (interface) to downstream systems... You will write a small "Provider" component (a single class will suffice) that "knows" the format for each of your different website poker history data providers, and exposes that data in exactly the same way to the upstream system that reads it...
听起来您需要策略模式,它允许您以多种不同的方式实现算法:
http: //en.wikipedia.org/wiki/Strategy_pattern
Sounds like you need the Strategy pattern, which allows you to implement an algorithm in a number of different ways:
http://en.wikipedia.org/wiki/Strategy_pattern
首先,创建您的“在线扑克手牌历史”模型。 该模型将表示数据,并且能够独立于源处理该数据。 然后为每种不同的源格式创建提供程序,这些提供程序必须能够将文件信息转换为模型。
编辑:例如
First, create your "Online Poker Hand History" model. This model will represent the data and will be able to process this data independently from the source. Then create providers for each of the different source formats that must be capable of converting the file's information into the model.
EDIT: e.g.
您还可以考虑使用 命令模式,其中您可以对文件类型的到达时间执行操作你需要处理。 这样您就可以灵活地适应所有格式,并遵守您的流程所需的一致参数。
另一个好处是您可以为每种新文件格式创建新的操作,而无需重构其他格式的代码。
You could also consider using the Command Pattern where you would have an Action for reach time of file type you need to process. This way you can have the flexibility for all the formats and adhere to the consistent parameters that your process will require.
Another benefit is that you can create new Actions for each new file format without refactoring the code for the other formats.