Re: Organizing A File Into Multiple Files
Re: Organizing A File Into Multiple Files
- Subject: Re: Organizing A File Into Multiple Files
- From: Chris Suter <email@hidden>
- Date: Wed, 10 Jan 2007 10:03:36 +1100
On 10/01/2007, at 9:18 AM, Matthew Callis wrote:
Hello!
I have a project that has grown far too large to maintain in one
file like I
currently have it. I don't know the best way to do what I want without
making a whole lot of major changes, if possible. Right now it's
one long
function, switched based on file extensions one after another. I
would like
for each file extension to be in its own file so it's much easier to
maintain and make changes and add new ones.
[snip]
You could try something like this:
// Abstract
@interface FileHandler : NSObject {
+ (NSString *)extension;
- (int)handleFile:(NSFileHandle *)fileHandle;
...
}
@end
Then make a subclass for each different extension you have and stick
them in a separate file.
Register them somewhere:
static NSMutableDictionary *myFileHandlers;
- (void)registerFileHandler:(Class)fileHandler
{
[myFileHandlers setObject:fileHandler forKey:[fileHandler
extension]];
}
- (void)registerFileHandlers
{
[self registerFileHandler:[XYZFileHandler class]];
...
}
Then instead of switching you'd do:
Class handlerClass = [myFileHandlers objectForKey:extension];
FileHandler *handler = [[[handlerClass alloc] init] autorelease];
if (!handler) {
...
} else
[handler handleFile:fileHandle];
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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