0

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!

3
  • Minor note, since you're new to this: fields at the top of a class with no visibility specifier are published by default, not private, which is probably what you want. Commented Oct 26, 2009 at 16:25
  • Did you try to use the XML Data Binding wizard? If you are new to Delphi OOP, it can save you a lot of time. It is mentioned in my 'Practical XML with Delphi' on-line session here (wiert.wordpress.com/2009/09/13/…) which has lots of references to online resources, and this Codegearguru video: codegearguru.com/video/039/XMLDataBinding.html I'm not completely sure the wizard does cardinality, but it is a good start to get the hang of OO. Commented Oct 26, 2009 at 17:21
  • @Mason: thanks for the warning. I keep forgetting that Delphi is not Java. :) @Jeroen: unfortunately, my XML needs seems to be too complex for the binding wizard. I've tried to run it through the wizard, but it fails rather badly, so I'm writing my own XSLT file to generate the necessary code. Commented Oct 29, 2009 at 7:53

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.