Objective-C - creating specific instances of a class from a base class depending on the type

Just to give an example in the real world, let's say the base class is the Vehicle and Concrete classes - TwoWheeler and FourWheeler. Now the type of vehicle - TwoWheeler or FourWheeler, is determined by the base class of the car. When I create an instance of TwoWheeler / FourWheeler using the alloc-init method, it calls a super implementation, as shown below, to set the value of the common properties defined in the Vehicle class, and of these properties, one of them is the type that actually solves is whether the type is TwoWheeler or FourWheeler.

   if (self = [super initWithDictionary:dict]){
     [self setOtherAttributes:dict]; 
      return self; 
    }

Now that I get a collection of cars, some of them may be TwoWheeler, while others may be FourWheeler. Therefore, I cannot directly create an instance of TwoWheeler or FourWheeler like this

Vehicle *v = [[TwoWheeler alloc] initWithDictionary:dict];

Is it possible to create an instance of a base class, and as soon as I know the type, create an instance of the child class depending on the type and return it. With the current implementation, this will lead to an infinite loop, because I am invoking a super implementation from a particular class.

What would be the perfect design to handle this scenario when I don't know which particular class should be created in advance?

+3
source share
2 answers

This is usually done using Factory .

, factory , , . Objective C .

+ (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];
    }
}

factory Vehicle .

// Instead of [[Vehicle alloc] initWithDictionary:dict]
Vehicle *vehicle = [Vehicle vehicleWithDictionary:dict];

, . , .

- (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;
}
+5

factory , Vehicle , createInstance, , , ,

+ (Vehicle*) createInstance:(int)numberOfWheels
{
    if(numberOfWheels == 2)
    {
        return [[TwoWheeler alloc] init];
    }
    else
    {
        return [[FourWheeler alloc] init];
    }

    return nil;
}

Vehicle *v = [Vehicle createInstance:2];
+2

Source: https://habr.com/ru/post/1539215/


All Articles