views:

101

answers:

1

I'm processing a XML with minOccurs and maxOccurs set frequently to either 0, 1, or unbounded. I have a schema describing this cardinality, together with the specific data type. I'd like to construct a (Delphi) class which keeps track of the cardinality, together with an array whose dimensions are to be validated based on the minOccurs and maxOccur fields. I believe using variants is a poor design choice, as I'll be fully aware of the data type before it's read (based on the XML schema rules).

I'm rather new to OOP in general, and Delphi OOP in particular, so what's my best options here? I've dreamt up something like:

TComplexType = class(TObject)
  FMinOccurs: integer;
  FMaxOccurs: integer;
  FValue:     Array of Variant;
public
  constructor Create(Min: integer; Max: integer);
  procedure AddValue(Value: variant);
  function Validate() : boolean;
end;

Of course, FValue may end up being a string, an integer, a double, etc. Thus, I believe I need to specialize:

TComplexString = class(TComplexType)
  FValue:     Array of string;
end;

Now, is the right way to go? Do I have to overload AddValue(Value: SomeType) in all the different classes (each class corresponding to a data type)? That doesn't seem very slick, as I'll be doing pretty much the same thing in every AddValue-method:

procedure AddValue(Value: SomeType);
begin;
  // 1) Re-shape array
  // 2) Add Value as the last (newly created) element in the array
end;

I'd really hate to do this for every type. (Admittedly, there won't be that many types, but I still consider it flawed design, as the logical content is pretty much identical in the overloaded methods.) Any good tips out there? Thanks!

+2  A: 

You didn't specify your Delphi version, but this is a classic example for generics (available in D2009 and higher).

Uwe Raabe
Thanks Uwe! Finally I've got a good reason to learn generics... :) I'll check it out.
conciliator