开发者

Create categories, inherance or delegate?

I am having a little problem. My scenario is: I will build a project with a lot of target. I want have the "public code" (for all target) and "specific code". The problem is: In "public code", I NEED call function at "specific code".

My first try was using categories. I create "public.h" then "public+specific.h" codes using categories. The class that will use this class will need to:

#import "public+specific.h"
...
public *myClass = [[public alloc] init];
[myclass doSomething];

To use another specific class, i only need to change the #import and nothing more. The unique problem is that in "public class" i will need create a false function, like.

//public.h
@interface public : NSObject {}
...
- (void) doSomething {return };

//public+specific.h
@interface public (specific)
...
 - (void) doSomething { //do what it really have to do };

The other problems is intrinsic to categories: I can't create local class variable, all will have to be declared in "public.h". I want have all specific things IN specific class...

Ok, so I try in another way: use Inheritance with delegates. In the classes "public.h" and "public+specific.h" it work very well, no need to use fake function, all was fine. BUT, (aways a but), I always will have to alloc the specific class, and if I don't want this, I can create a fake function only to call the delegate, so I have the same problem above. This is a sample:

  //In public.h
  @protocol publicDelegate
    -(void)doSomething;
  @end
  @interface public : NSObject {
      id <publicDelegate> myDelegate;
   }
   -(id)initWithDelegate (id <publicDelegate>)initDelegate{
       myDelegate = initDelegate;
       [myDelegate doSomehing];
   }

   //public+specific.h   //The '+' isn't correct here :P
   #include public.h
   @interface public_specific : public <publicDelegate> {}
   - (id)init{
        return [super initWithD开发者_开发百科elegate:self];
   }  
   - (void) doSomething { //do what it really have to do };

Like I say, the problem here is how I create this object

   #import "public+specific.h"
   ...
   public_specific *myClass = [[public_specific alloc] init];
   [myClass doSomething];

With this, I will have to create a lot of #if defined , #elif defined... every time that I need to create a object call. With categories, I only need to do this with the "#include".

To solve this problem, I can have things like this:

    //in "public.h"
    - (void) doSomething { 
         return [myDelegate doSomehing]
    };

Another time I will create fake function. And worst, for every new function in "public+specific.h" I will have to create another fake function.. zzz.. (in categories, i have to do this only with function with "public.h" call in "public+specific.h")

So, anyone have another idea to this problem?? It's a little problem, but I want to make my code good, easy to develop and clean...


in many cases, composition would serve you well.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜