开发者

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 s开发者_运维知识库ignature 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜