Do you recommend a design pattern to better wrte this code?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Thanks_forHelp
Posted On:   Thursday, June 7, 2007 07:10 PM

I am writing a Java program that read through a file that have various records types identified by the first letter. E.g. Contents of data.txt: person, John Smith, 1 jan 1960, 501-123-4567 education, Bsc, Wales University, 1 may 1985 address, 123 main street, Pal alto, Fl, 87321 record = readLine(); id = record.split(",")[0] if (id.equal("person")) { Person p = new Person(record); doSomethingWith(p); } else if (id.equal("education") { Education e = new Education(record); doSomethingWith(p); .... Is there any design pattern that would address such type of coding better?    More>>
			

I am writing a Java program that read through a file that have various records types identified by the first letter. E.g.

Contents of data.txt:
person, John Smith, 1 jan 1960, 501-123-4567
education, Bsc, Wales University, 1 may 1985
address, 123 main street, Pal alto, Fl, 87321

record = readLine();
id = record.split(",")[0]
if (id.equal("person")) {
Person p = new Person(record);
doSomethingWith(p);
} else if (id.equal("education") {
Education e = new Education(record);
doSomethingWith(p);
....

Is there any design pattern that would address such type of coding better?
   <<Less

Re: Do you recommend a design pattern to better wrte this code?

Posted By:   Anonymous  
Posted On:   Tuesday, June 12, 2007 03:09 PM

There's room for improvement there, but that depends on what you call "coding better". I think that the biggest concern with that code should be the way you hard-coded the relationship between IDs and classes. The simplest way to improve that is to have a property file somewhere, specifying this relationship.



For demonstration purposes, I will assume that the classes Person and Education both extend the class Something and that they all reside in the package "your.package". In that case, the property file would look like this:

person=your.package.Person
education=your.package.Education



And your code above becomes:

Properties props = new Properties();
props.load(someStream);
record = readLine();
id = record.split(",")[0];
Something something = null;
try {
something = Class.
forName(props.getProperty(id)).
getConstructor(record.getClass()).
newInstance(record);
} catch (Exception e) {
// handle exceptions here
}
doSomethingWith(something);



I hope that helps :)

Re: Do you recommend a design pattern to better wrte this code?

Posted By:   Robert_Lybarger  
Posted On:   Thursday, June 7, 2007 08:05 PM

For something like that (file parsing) nothing really comes to mind. You're going to do a string-based if/elseif/else comparison at some point ... what you have already at least has the benefit of being rather immediately clear. If you have a *bunch* of class types to check against, though, then you might worry more about it. Still, I'll be watching to see if/what someone else says... maybe I can pick up a good trick myself.
About | Sitemap | Contact