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?
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
You would call it like this
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.
The factory can be part of the Vehicle class and used as so.
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.