I have a base class that has class methods defined for returning various pieces of information that I use to build service url’s. The base class also has an instance method defined for building that URL. I would like the base class to have a default implementation of this method, so that I only have to override it in my subclasses when I need the url to be different. How do I have this base method call the overridden class methods to build the url using the class methods from the subclass? Below is the code I have now that is not working:
The base class method:
- (NSString *)buildRequestUrlString{
return [Utils serviceEndpointForOperationId:[[self class]operationId] version:[[self class] operationVersion] andRequestMethod:[[self class] methodType];
}
operationId, operationVersion and methodType are the class methods that are implemented in the subclass, but when the subclass makes the following call:
[super buildRequestUrlString];
It is using the class methods from the base class to build my endpoint, and not the class methods in my subclass. How do I make it not do that? Is it even possible?
Thanks in advance,
Nick
I would use the template pattern for this. Basically create 3 instance methods in your base class to get your operationId, etc. in the base class they just return nil. Then in your subclasses override these 3 methods to return the correct values.
Your buildRequestUrlString method uses the [self operationId] etc.