设计模式:解析文本文件中相似但不同的模式

发布于 2024-07-10 04:31:58 字数 330 浏览 5 评论 0原文

在此先感谢您的帮助。 我想知道是否有一种(设计)模式可以应用于这个问题。

我希望从具有相似但不同格式的文本文件中解析、处理和提取值。

更具体地说,我正在构建一个处理引擎,它接受来自多个不同格式的在线扑克手牌历史记录文件网站并解析出特定的数据字段(手号、日期时间、玩家)。 对于每种格式,我需要解析文件的逻辑略有不同,但提取值的处理将是相同的。

我的第一个想法是创建一个类,它接受每种文件类型的“模式”并相应地进行解析/处理。 我确信对此有更好的解决方案。

谢谢!

奖励积分: 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 技术交流群。

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

发布评论

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

评论(5

玻璃人 2024-07-17 04:31:58

这听起来像是策略模式的候选者。 可以在此处找到一个 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.

墨小墨 2024-07-17 04:31:58

“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...

浮生未歇 2024-07-17 04:31:58

听起来您需要策略模式,它允许您以多种不同的方式实现算法:

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

泪痕残 2024-07-17 04:31:58

首先,创建您的“在线扑克手牌历史”模型。 该模型将表示数据,并且能够独立于源处理该数据。 然后为每种不同的源格式创建提供程序,这些提供程序必须能够将文件信息转换为模型。

编辑:例如

public interface IDataProvider
{
    IOnlinePokerInfo ParseFileInformation(FileInfo input);
}

public interface IOnlinePokerInfo
{
    int Hand { get; set; }
    DateTime DateInfo { get; set; }
    List<IPlayer> Players { get; set; }
    void ProcessInformation();
}

public interface IPlayer
{
    string Name { get; set; }
}

public class MyOnlinePokerInfo : IOnlinePokerInfo
{
    private int hand;
    private DateTime date;
    private List<IPlayer> players;

    public int Hand { get { return hand; } set { hand = value; } }
    public DateTime DateInfo { get { return date; } set { date = value; } }
    public List<IPlayer> Players { get { return players; } set { players = value; } }

    public MyOnlinePokerInfo(int hand, DateTime date)
    {
        this.hand = hand;
        this.date = date;
        players = new List<IPlayer>();
    }

    public MyOnlinePokerInfo(int hand, DateTime date, List<IPlayer> players)
        : this(hand, date)
    {
        this.players = players;
    }

    public void AddPlayer(IPlayer player)
    {
        players.Add(player);
    }

    public void ProcessInformation()
    {
        Console.WriteLine(ToString());
    }

    public override string ToString()
    {
        StringBuilder info = new StringBuilder("Hand #: " + hand + " Date: " + date.ToLongDateString());
        info.Append("\nPlayers:");
        foreach (var s in players)
        {
            info.Append("\n"); 
            info.Append(s.Name);
        }
        return info.ToString();
    }
}

public class MySampleProvider1 : IDataProvider
{
    public IOnlinePokerInfo ParseFileInformation(FileInfo input)
    {
        MyOnlinePokerInfo info = new MyOnlinePokerInfo(1, DateTime.Now);
        IPlayer p = new MyPlayer("me");
        info.AddPlayer(p);
        return info;
    }
}

public class MySampleProvider2 : IDataProvider
{
    public IOnlinePokerInfo ParseFileInformation(FileInfo input)
    {
        MyOnlinePokerInfo info = new MyOnlinePokerInfo(2, DateTime.Now);
        IPlayer p = new MyPlayer("you");
        info.AddPlayer(p);
        return info;
    }
}

public class MyPlayer : IPlayer
{
    private string name;
    public string Name { get { return name; } set { name = value; } }

    public MyPlayer(string name)
    {
        this.name = name;
    }
}

public class OnlinePokerInfoManager
{
    static void Main(string[] args)
    {
        List<IOnlinePokerInfo> infos = new List<IOnlinePokerInfo>();

        MySampleProvider1 prov1 = new MySampleProvider1();
        infos.Add(prov1.ParseFileInformation(new FileInfo(@"c:\file1.txt")));

        MySampleProvider2 prov2 = new MySampleProvider2();
        infos.Add(prov2.ParseFileInformation(new FileInfo(@"c:\file2.log")));

        foreach (var m in infos)
        {
            m.ProcessInformation();
        }
    }
}

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.

public interface IDataProvider
{
    IOnlinePokerInfo ParseFileInformation(FileInfo input);
}

public interface IOnlinePokerInfo
{
    int Hand { get; set; }
    DateTime DateInfo { get; set; }
    List<IPlayer> Players { get; set; }
    void ProcessInformation();
}

public interface IPlayer
{
    string Name { get; set; }
}

public class MyOnlinePokerInfo : IOnlinePokerInfo
{
    private int hand;
    private DateTime date;
    private List<IPlayer> players;

    public int Hand { get { return hand; } set { hand = value; } }
    public DateTime DateInfo { get { return date; } set { date = value; } }
    public List<IPlayer> Players { get { return players; } set { players = value; } }

    public MyOnlinePokerInfo(int hand, DateTime date)
    {
        this.hand = hand;
        this.date = date;
        players = new List<IPlayer>();
    }

    public MyOnlinePokerInfo(int hand, DateTime date, List<IPlayer> players)
        : this(hand, date)
    {
        this.players = players;
    }

    public void AddPlayer(IPlayer player)
    {
        players.Add(player);
    }

    public void ProcessInformation()
    {
        Console.WriteLine(ToString());
    }

    public override string ToString()
    {
        StringBuilder info = new StringBuilder("Hand #: " + hand + " Date: " + date.ToLongDateString());
        info.Append("\nPlayers:");
        foreach (var s in players)
        {
            info.Append("\n"); 
            info.Append(s.Name);
        }
        return info.ToString();
    }
}

public class MySampleProvider1 : IDataProvider
{
    public IOnlinePokerInfo ParseFileInformation(FileInfo input)
    {
        MyOnlinePokerInfo info = new MyOnlinePokerInfo(1, DateTime.Now);
        IPlayer p = new MyPlayer("me");
        info.AddPlayer(p);
        return info;
    }
}

public class MySampleProvider2 : IDataProvider
{
    public IOnlinePokerInfo ParseFileInformation(FileInfo input)
    {
        MyOnlinePokerInfo info = new MyOnlinePokerInfo(2, DateTime.Now);
        IPlayer p = new MyPlayer("you");
        info.AddPlayer(p);
        return info;
    }
}

public class MyPlayer : IPlayer
{
    private string name;
    public string Name { get { return name; } set { name = value; } }

    public MyPlayer(string name)
    {
        this.name = name;
    }
}

public class OnlinePokerInfoManager
{
    static void Main(string[] args)
    {
        List<IOnlinePokerInfo> infos = new List<IOnlinePokerInfo>();

        MySampleProvider1 prov1 = new MySampleProvider1();
        infos.Add(prov1.ParseFileInformation(new FileInfo(@"c:\file1.txt")));

        MySampleProvider2 prov2 = new MySampleProvider2();
        infos.Add(prov2.ParseFileInformation(new FileInfo(@"c:\file2.log")));

        foreach (var m in infos)
        {
            m.ProcessInformation();
        }
    }
}
勿忘初心 2024-07-17 04:31:58

您还可以考虑使用 命令模式,其中您可以对文件类型的到达时间执行操作你需要处理。 这样您就可以灵活地适应所有格式,并遵守您的流程所需的一致参数。

另一个好处是您可以为每种新文件格式创建新的操作,而无需重构其他格式的代码。

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.

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