I'm learning OO principles and for practice I'm developing an application in Java.
The application is to parse the hand history text files issued by poker sites after every poker hand is played and to extract the same types of data from the file no matter which site the hand history is from.
The hand history files can be in very different formats depending on the poker site. Some sites have each hand in a very human-readable style eg:
NL $0.25/$0.50 Texas Hold'em - Tuesday, June 22, 19:55:24 GMT 2010
and others have an XML style.
My question is: When parsing the above line from the text, should I have separate classes to parse each element? For instance should I have a StakesParser class with a parseStakes() method and then a CurrencyParser class with a parseCurrency() method etc. Or should I have a single class that can parse that line and get all the different bits of info from it?
To have a separate class for each thing I want to parse seems more OO, but less efficient. Any advice?
What I want as a result of the parsing process is a GameState class which would hold all the info for one hand of Poker. So no matter what site the hand history file was from I would be able to make an object of type GameState with all the relevant info parsed for it by an HHParser class.
To this end, I thought it would be a good idea to have an interface which would have to be implemented which would ensure that the HHParser for each poker site would end up producing a GameState class. So the interface would have methods such as parseStakes(), parseCurrency() etc. Is this the right approach?
What makes it more confusing for me is that on different sites, the information is in a different order so I possibly need a controller class that makes sure that each method is called in the right order depending on what site the text file is from.
Sorry about the waffle, but I'd be grateful for any pointers on what is might be best to do.
Many thanks in advance. :-)
I wonder if a "class per element" strategy is too granular. I'm thinking maybe some sort of factory that returns the correct parser implementation for a particular site.
public class ParserFactory {
public static Parser get(String url) {...};
}
So you end up with a Parser interface responsible for returning some sort of java bean encapsulating the attributes of a poker hand.
public interface Parser {
public Collection<Hand> parse();
}
And you implement Parser for each site from which you can obtain hands, returning the correct one from the factory. I might use Commons Digester for the XML sites and ANTLR or even regular expressions for the non-XML ones.
Since it is just a single line of data, I recommend having a single parser object that deals with parsing out the currency, stakes, etc. This class could then instantiate different objects to hold the data.
This does not violate OO principles since the parser class has a single responsibility of reading text data and generating Java objects from it. And each Java object has the responsibility of holding it's given data for use by your program.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With