• 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
Re: To subclass or not to subclass?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: To subclass or not to subclass?


  • Subject: Re: To subclass or not to subclass?
  • From: Ondra Cada <email@hidden>
  • Date: Wed, 28 Nov 2001 17:42:22 +0100

Carlo,

>>>>>> Carlo Volpi (CV) wrote at Wed, 28 Nov 2001 12:11:57 +0100:
CV> The first solution represents better the real-world math but creates a
CV> relatively big number of classes each of them with just one instance.
CV> The second solution instead creates a big number of methods for the
CV> Operator class but resolves in just one operator class with as many
CV> instances as the number of operators I want to use.
CV>
CV> >From what I understood with the first solution it would be easier to
CV> >create
CV> NSBundles that contain new operators, but I'd really like to hear the
CV> opinion of more experienced programmers before going on.

Perhaps I am overlooking some much better solution, but I would personally
go the first solution -- more or less.

Instead of forcing the operator objects to be instances of some particular
class, I would want them just to respond to some selectors (let's say, name,
info, numberOfArguments, compute). That way, a majority of operator objects
won't need to be _instances_ at all -- classes will be sufficient:

@implementation JustPlainIntegerAddAsExample
+(NSString*)name { return @"+"; }
+(NSString*)info { return @"blah blah blah"; }
+(int)numberOfArguments { return 2; }
+compute:(NSArray*)args {
return [NSNumber numberWithInt:[[args objectAtIndex:0] intValue]+[[args
objectAtIndex:1] intValue]];
}
@end

Of course, for more complicated operators instances could be used as well:

@interface CachedAdd_QuiteNonsensical:... {
NSMutableDictionary *cache;
}
@end
@implementation CachedAdd_QuiteNonsensical
-init {
if ((self=[super init])==nil) return nil
cache=[[NSMutableDictionary alloc] init];
return self;
}
-(void)dealloc {
[cache release];
[super dealloc];
}
-(NSString*)name { return @"+"; }
-(NSString*)info { return @"blah blah blah"; }
-(int)numberOfArguments { return 2; }
-compute:(NSArray*)args {
NSString *key=[args description];
id result=[cache objectForKey:key];
if (!result) {
result=[NSNumber numberWithInt:[[args objectAtIndex:0] intValue]+[[args
objectAtIndex:1] intValue]];
[cache setObject:result forKey:key];
}
return result;
}
@end
---
Ondra Cada
OCSoftware: email@hidden http://www.ocs.cz
2K Development: email@hidden http://www.2kdevelopment.cz
private email@hidden http://www.ocs.cz/oc


References: 
 >To subclass or not to subclass? (From: Carlo Volpi <email@hidden>)

  • Prev by Date: First Window?
  • Next by Date: NSTextField resizing
  • Previous by thread: To subclass or not to subclass?
  • Next by thread: Equivalent to gethrtime()
  • Index(es):
    • Date
    • Thread