ios - Is it better programming practice to test in a superclass if it is a subclass and then complete actions or to have those seperate -


specifically, have 2 different playingcardviewcontrollers have 2 different cardgames. both setcardgameviewcontroller , playingcardgameviewcontroller inherit general cardvc.

is better practice test in cardvc if class either of subclasses , complete actions needed each subclasses? mean there less code, seems might confusing. should best practice depend on how similar methods between 2 classes? asking reference using iskindofclass:

for example if ([self iskindofclass:[playingcardvc1 class]])

since asked more specific question here's more specific question. better put following method in subclass way i've defined , not implement in subclasses? or better practice set method in superclass cardvc nil, , implement separately in each of subclasses?

-(bool)inserthighscore:(highscore*)testedhighscore {  nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults];  nsmutablearray *cardhighscores; // check if class setcardvc if ([self iskindofclass:[setcardgameviewcontroller class]]){     if (![defaults objectforkey:@"setcardhighscores"]){ // if nil         cardhighscores = [[nsmutablearray alloc]init];         [defaults setobject:cardhighscores forkey:@"setcardhighscores"];     }     cardhighscores = [defaults objectforkey:@"setcardhighscores"]; } // check if class playingcardvc else if([self iskindofclass:[playingcardgameviewcontroller class]]){     // ignored thing     if (![defaults objectforkey:@"playingcardhighscores"]){ // if nil         cardhighscores = [[nsmutablearray alloc]init];         [defaults setobject:cardhighscores forkey:@"playingcardhighscores"];     }     nslog(@"finding defaults count == %lu",(unsigned long)[(nsmutablearray *)[defaults objectforkey:@"playingcardhighscores"]count]);     cardhighscores = [defaults objectforkey:@"playingcardhighscores"]; } //... rest of code 

tommy's solution one. here's alternative solution.

declare method in superclass take key parameter. call superclass method subclasses different keys. superclass implementation this

- (bool)inserthighscore:(highscore *)testedhighscore forkey:(nsstring *)key {     nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults];      nsmutablearray *cardhighscores;      if (![defaults objectforkey:key]){         cardhighscores = [[nsmutablearray alloc]init];         [defaults setobject:cardhighscores forkey:key];     }     cardhighscores = [defaults objectforkey:key];      // ... rest of code } 

the subclass implementation this

- (bool)inserthighscore:(highscore *)testedhighscore {     return [super inserthighscore:testedhighscore forkey:@"setcardhighscores"]; } 

in general, code specific subclass goes in subclass implementation, code common subclasses goes superclass implementation, , can pass subclass-specific information superclass through parameters. in example above, superclass doesn't need know subclasses. knows subclasses give key in nsuserdefaults.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -