Just to give a real world example, say the base class is Vehicle and concrete classes are TwoWheeler and FourWheeler. Now the type of the vehicle - TwoWheeler or FourWheeler, is decided by the base class Vehicle. When I create an instance of TwoWheeler/FourWheeler using alloc-init method, it calls the super implementation like below to set the value of common properties defined in the Vehicle class and out of these properties one of them is type that actually decides if the type is TwoWheeler or FourWheeler.
if (self = [super initWithDictionary:dict]){
[self setOtherAttributes:dict];
return self;
}
Now when I get a collection of vehicles some of them could be TwoWheeler and others will be FourWheeler. Hence I cannot directly create an instance of TwoWheeler or FourWheeler like this
Vehicle *v = [[TwoWheeler alloc] initWithDictionary:dict];
Is there any way I can create an instance of base class and once I know the type, create an instance of child class depending upon type and return it. With the current implementation, it would result in infinite loop because I call super implementation from concrete class.
What would be the perfect design to handle this scenario when I don't know which concrete class should be instantiated beforehand?
Generally this is done with a Factory.
If you want the factory to be part of the base class, that's fine but it may cause issues in the future. In Objective C, class methods make good factories.
+ (Vehicle *)vehicleWithDictionary:(NSDictionary *)dict
{
if ([[dict objectForKey:kVehicleType] isEqualToString:@"TwoWheeler"]) {
return [[[TwoWheeler alloc] initWithDictionary:dict] autorelease];
} else if ([[dict objectForKey:kVehicleType] isEqualToString @"FourWheeler"]) {
return [[[FourWheeler alloc] initWithDictionary:dict] autorelease];
} else {
return [[[Vehicle alloc] initWithDictionary:dict] autorelease];
}
}
The factory can be part of the Vehicle class and used as so.
// Instead of [[Vehicle alloc] initWithDictionary:dict]
Vehicle *vehicle = [Vehicle vehicleWithDictionary:dict];
Update
I came up with a way to do what was asked. Let it serve as a shining example of why it's such a bad idea and why you should never do it.
- (id)initWithDictionary:(NSDictionary *)dict
{
self = [super init];
if (self) {
// If override is in the dictionary, then it mustn't try to call the subclass.
if (![dict objectForKey:kOverride]) {
NSMutableDictionary *overrideDict = [NSMutableDictionary dictionaryWithDictionary:dict];
[overrideDict setObject:@"override" forKey:kOverride];
if ([[dict objectForKey:kVehicleType] isEqualToString:@"TwoWheeler"]) {
[self release];
return [[[TwoWheeler alloc] initWithDictionary:overrideDict] autorelease];
} else if ([[dict objectForKey:kVehicleType] isEqualToString @"FourWheeler"]) {
[self release];
return [[[FourWheeler alloc] initWithDictionary:overrideDict] autorelease];
}
}
// init as normal
}
return self;
}
you should use abstract factory As following, the class Vehicle will have a method called, createInstance, this method will have a parameter that will decide what to create consider the example
+ (Vehicle*) createInstance:(int)numberOfWheels
{
if(numberOfWheels == 2)
{
return [[TwoWheeler alloc] init];
}
else
{
return [[FourWheeler alloc] init];
}
return nil;
}
You would call it like this
Vehicle *v = [Vehicle createInstance:2];