Thursday, February 10, 2011

Polymorphism with different method signatures

I have a group of classes (say for validation rules). Each one returns a true or false.

I use id and call a method signature for each one of the classes and get the results allowing me to dynamically create validation rules.

Worked great until...

I have a new class that takes an extra parameter to come up with its validation.

What is the best way to deal with this?

Modify every other classes method signature to take a parameter that they don't need?

  • Probably the most appropriate course of action is to abstract your parameter passing into an object that can have a variable profile of variables.

    Of course, more simply, Objective-C does allow for a variable parameter list much like C:

    void method(int a, ...)     // in C
    - (void) method:(id) firstObject, ...  // in ObjC
    

    Apple has Technical Q&A on the very subject.

    Jon Reid : Yes, putting the variables into an NSDictionary would let the various classes pick & choose what they're interested in.
    From rcw3

0 comments:

Post a Comment