I plan to override a method in the subclass if a boolean is set to a certain value, and then to switch this value, and have thought of two options:
1. Calling super and checking success
// Superclass
-(BOOL) showEngine
BOOL result = !self.isEngineActive;
if (result) {
// Does something common to both super and subclass
self.isEngineActive = YES;
}
return result;
}
// Subclass override
-(BOOL) showEngine
BOOL result = [super showEngine];
if (result) {
// Does something unique to subclass
}
return result;
}
2. Delegate to a second method
// Superclass
-(void) showEngine
if (!self.isEngineActive) {
// Delegate
showEngineNow();
// Does something common to both super and subclass
self.isEngineActive = YES;
}
}
// Subclass override
-(void) showEngineNow() {
[super showEngineNow];
// Does something unique to subclass
}
The first method has the negative effect of having to call super and check the result, whereas the second has the negative baggage of a second method call to the delegate. Which would be the better way, and is there one I haven’t thought of?
Calling the super implementation is pretty common when overriding a method. I don’t see this as a negative. It’s not clear from the question what you are trying to acheive, but I’d go with option 1 – why introduce more complexity?
This is assuming that the unspecified
//stuffyou are doing in the super implementation should also be done by the subclass. If not, you will need to split out into separate methods.I don’t think you are using “delegate” as it is intended in Cocoa. If it’s just another method in the same class, it isn’t a delegate. A delegate is a separate object that conforms to a known protocol.
If you feel that the methods are getting too long and doing too many things, then by all means break them up and override only the relevant parts in your subclass. In that case option 2 is your best bet, but don’t call it a delegate.