I have a protocol P
@protocol P<NSObject>
{
-(void)foo;
@optional
-(void)bar;
}
And I have bunch of classes (let say a dozen). All of these classes implement protocol P. About half of them implement method bar and all of bar implementations are exactly the same.
What is the best way to share implementation of bar?
Obvious ideas:
1) Create some base class which will implement method bar and let other classes to inherit it.
It's simple to implement. However, I am not big fan of this idea. I prefer class hierarchy to represent entity generalization/specification rather than code reuse.
2) Create a helper and call it from all of classes which needs to implement bar method
Ok. It works. However, if implementation of bar is small (couple of lines in my case) then we will have more overhead (helper class and calling it from each class) than the code itself.
Is there any other (better) methods?