• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
OO idiom avoiding switch and if
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

OO idiom avoiding switch and if


  • Subject: OO idiom avoiding switch and if
  • From: Jeffrey Oleander <email@hidden>
  • Date: Thu, 9 Apr 2009 18:45:34 -0700 (PDT)

There's a programming idiom to avoid using complex if statements and switch/case statements, and instead to just send a message to a different class of object.

I'm doing some parsing of an old text data format which
has a hierarchy with a record and then sub-records and
sub-sub records, 1 per "line".  Each is structured as
a type, nesting level number and then various kinds of
values depending thereon.  What I'm agonizing over is
how best to handle invoking the processing for each
sub-record type in Objective-C.  This would seem to
require having a bunch of classes with names like
JGRecordTypeParser and then I might do something like:
NSString * valueParserClassPrefix = @"JG";
NSString * valueParserClass = [[valueParserClassPrefix stringByAppendingString:recordSubType] stringByAppendingString:@"Parser"];
[[NSClassFromString(valueParserClass) alloc] initWithTokens:tokens  recordType:recordType level:aLevel];
or some such.

Or, I could just do it all in my primary parser class
[self parseValueOfType:recordType subRecordType:subType level:aLevel];
and then have the rat's nest inside there:
switch (recodType)
{
  case thisType:
   //...
   break;
  case thatType:
   //...
   break;
  default:
  //...
}

Advice on what's least messy overall or pointers
to sources of info would be appreciated.



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: OO idiom avoiding switch and if
      • From: Sherm Pendley <email@hidden>
  • Prev by Date: Re: Sqlite3
  • Next by Date: Re: OO idiom avoiding switch and if
  • Previous by thread: Re: static method to create CABasicAnimation: does this create a memory leak?
  • Next by thread: Re: OO idiom avoiding switch and if
  • Index(es):
    • Date
    • Thread