Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data provider calling a delegate: specifics or generic?

I have a XML parser which will parse 17 different XML documents (I'm simplifying this). When the parser has finished its job, it calls the object that did the request.

First way

A single method that looks like

- (void)didReceiveObject:(NSObject *)object ofType:(MyObjectType)type

with MyObjectType being an enum.

In this method, I check the type and redirect the object to the corresponding method.

Second way

There is a callback method for each of the 17 types of object I can receive.

- (void)didReceiveFoo:(MYFoo *)foo
- (void)didReceiveBar:(MYBar *)bar
... and so on

Which way of using delegates will be better? We had a discussion about this with a colleague and couldn't find one way more appealing than another. It seems like it's just deciding what method to call from the parser or within the delegate....

Even when thinking about adding future methods/delegates callbacks, we don't see any real problem.

Is one of these ways better than the other? Is there another way?

like image 992
teriiehina Avatar asked May 24 '26 01:05

teriiehina


1 Answers

Why not go with

- (void)didReceiveObject:(NSObject *)object

and then inspect the class type?

This seems cleaner and more extensible to me, because it means you can parse other objects in the future without adding more callbacks.

(I know this is the same as option one, but I wanted to point out that your second argument was unnecessary.)

like image 88
Evan Cordell Avatar answered May 26 '26 19:05

Evan Cordell