 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Rod Guest
|
Posted: Wed Jun 21, 2006 10:02 pm Post subject: Need advice: OOP approach to read EDI file |
|
|
Hi.
I'm creating a program that will convert one text file layout into another (for EDI between a bank and an ERP system).
Below are two approaches. Which one you experts think is the way to go ?
Line read (layout = record id, date, name, foo data, foo data)
---------
020060621John Doe 333444
Solution 1
----------
TBaseField = class
private
FValue:string;
public
constructor Create(line:string);virtual;abstract;
property Value:string read FValue;
end;
TFieldRecordId = class(TBaseField)
constructor Create(line:string);override;
end;
TFieldDate = class(TBaseField)
constructor Create(line:string);override;
end;
TFieldName = class(TBaseField)
constructor Create(line:string);override;
end;
//...one class for each Field, a lot of work!
constructor TFieldRecordId.Create(line: string);
begin
inherited;
FValue:=Copy(line,1,1);
end;
constructor TFieldDate.Create(line: string);
begin
inherited;
FValue:=Copy(line,2, ;
end;
constructor TFieldName.Create(line: string);
begin
inherited;
FValue:=Copy(line,10,10);
end;
Solution 2
----------
TSimpleField = class
private
FName:string;
FStart,FEnd:integer;
public
constructor Create(name:string;start_pos,end_pos:integer);
function Value(line:string):string;virtual;
end;
function TSimpleField.Value(line: string): string;
begin
Result:=Copy(line,FStart,FEnd-FStart+1);
end;
Using Solution 1
----------------
var
c1:TFieldRecordId;
c2:TFieldDate;
c3:TFieldName;
begin
c1:=TFieldRecordId.Create(edit1.text);
c2:=TFieldDate.Create(edit1.text);
c3:=TFieldName.Create(edit1.Text);
memo1.Lines.Add(c1.Value);
memo1.Lines.Add(c2.Value);
memo1.Lines.Add(c3.Value);
Using Solution 2
----------------
var
c1,c2,c3:TSimpleField;
begin
c1:=TSimpleField.Create('Record ID',1,1);
c2:=TSimpleField.Create('Date',2,9);
c3:=TSimpleField.Create('Name',10,19);
memo2.lines.add(c1.Value(edit1.text));
memo2.lines.add(c2.Value(edit1.text));
memo2.lines.add(c3.Value(edit1.Text));
....
Thanks,
Rod. |
|
| Back to top |
|
 |
Jim Cooper Guest
|
Posted: Wed Jun 21, 2006 11:01 pm Post subject: Re: Need advice: OOP approach to read EDI file |
|
|
| Quote: | Below are two approaches. Which one you experts think is the way to go ?
|
I have to do a very similar thing, reading a set of fixed width fields off a
list of text lines, and turning them into XML (among other things).
I was going to have a collection of data objects like your TSimpleField. Not
sure yet if the actual list will be defined inside the parser class or outside
and passed in (I'm generally in favour of the latter, if only for easier unit
testing).
I could then have a set of methods to get values (I don't think overloading the
methods where only the return type is different works in Delphi) :
function GetStringValue(FieldName : string) : string;
function GetIntegerValue(FieldName : string) : integer;
... etc
That way I get a small number of classes and a small parser. I'll let conversion
exceptions get thrown if someone is dopey about calling the wrong one on a
field. I need to handle empty strings in a somewhat odd way sometimes, though,
which might complicate matters slightly.
Cheers,
Jim Cooper
_____________________________________________
Jim Cooper jcooper (AT) tabdee (DOT) ltd.uk
Skype : jim.cooper
Tabdee Ltd http://www.tabdee.ltd.uk
TurboSync - Connecting Delphi to your Palm
_____________________________________________ |
|
| Back to top |
|
 |
Charles Cooke Guest
|
Posted: Thu Jun 22, 2006 7:08 pm Post subject: Re: Need advice: OOP approach to read EDI file |
|
|
Rod wrote:
| Quote: | Hi.
I'm creating a program that will convert one text file layout into another (for EDI between a bank and an ERP system).
Below are two approaches. Which one you experts think is the way to go ?
Line read (layout = record id, date, name, foo data, foo data)
---------
020060621John Doe 333444
Solution 1
----------
TBaseField = class
private
FValue:string;
public
constructor Create(line:string);virtual;abstract;
property Value:string read FValue;
end;
TFieldRecordId = class(TBaseField)
constructor Create(line:string);override;
end;
TFieldDate = class(TBaseField)
constructor Create(line:string);override;
end;
TFieldName = class(TBaseField)
constructor Create(line:string);override;
end;
//...one class for each Field, a lot of work!
constructor TFieldRecordId.Create(line: string);
begin
inherited;
FValue:=Copy(line,1,1);
end;
constructor TFieldDate.Create(line: string);
begin
inherited;
FValue:=Copy(line,2, ;
end;
constructor TFieldName.Create(line: string);
begin
inherited;
FValue:=Copy(line,10,10);
end;
Solution 2
----------
TSimpleField = class
private
FName:string;
FStart,FEnd:integer;
public
constructor Create(name:string;start_pos,end_pos:integer);
function Value(line:string):string;virtual;
end;
function TSimpleField.Value(line: string): string;
begin
Result:=Copy(line,FStart,FEnd-FStart+1);
end;
Using Solution 1
----------------
var
c1:TFieldRecordId;
c2:TFieldDate;
c3:TFieldName;
begin
c1:=TFieldRecordId.Create(edit1.text);
c2:=TFieldDate.Create(edit1.text);
c3:=TFieldName.Create(edit1.Text);
memo1.Lines.Add(c1.Value);
memo1.Lines.Add(c2.Value);
memo1.Lines.Add(c3.Value);
Using Solution 2
----------------
var
c1,c2,c3:TSimpleField;
begin
c1:=TSimpleField.Create('Record ID',1,1);
c2:=TSimpleField.Create('Date',2,9);
c3:=TSimpleField.Create('Name',10,19);
memo2.lines.add(c1.Value(edit1.text));
memo2.lines.add(c2.Value(edit1.text));
memo2.lines.add(c3.Value(edit1.Text));
...
Thanks,
Rod.
|
Hi Rod,
Check out Martin Fowler's post on Language Workbenches
(http://martinfowler.com/articles/languageWorkbench.html) for a good OOP
method for what you're doing. It's more abstract/extensible, perhaps to
the point of excess depending on what you're going to be doing, but the
concepts are applicable.
--
Charles Cooke
Bamboo Architects, Inc.
1657 Barrington St, Suite 323
Halifax, NS B3J 2A1 Canada |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|