views:

42

answers:

0

I want to create a factory that:
- would create instance of object for some id (string),
- would allow for class to register itself for an id.

Currently this is implemented with centralized register of mapping.

I would like to decentralize the register, so every class would register itself in its implementation file.

My idea for this is creating some class and registering classes for ids using categories. For example if I would have factory class Factory and want to register class SomeClass for id @"someClassId" I would do this like this:

@interface Factory (SomeClass)
+ (id)someClassId;
@end

@implementation Factory (SomeClass)
+ (id)someClassId {
    return [[[SomeClass alloc] init] autorelease];
}
@end

Factory would of course have a method that would call proper method for id given as string.

The questions are:
- what pros/cons do you see in such solution?
- do you know any alternative solutions?

Cheers Sfider