Ravi Kiran
Ravi Kiran

Reputation: 229

I am creating a Framework in Objective C, Which needs a good constructor pattern

I am creating a Framework in objective C , which has multiple features, where all the features are need to be initialized while instatiating the library instance, where I am also giving flexibility of providing alternate implementation to any of the feature, to acheive this I am using builder pattern as follow, My question is, Which better Design pattern suits here? Or how Can I improve performace/Memory usage, Please suggest me some pattern for this problem

-(instancetype)initWithBuilder:(LibraryBuilder *)builder {
    self = [super init];
    if (self) {
        // allocate feature1 instance
        if (builder.feature1)
            self.feature1 = builder.feature1;
        else
            self.feature1 = [[InterfaceA alloc] initWithLib:self];

        // allocate feature2 instance
        if (builder.feature2)
            self.feature2 = builder.feature2;
        else
            self.feature2 = [[InterfaceB alloc] init];

        // allocate App Configuration instance
        if (builder.feature3)
            self.feature3 = builder.feature3;
        else
            self.feature3 = [[InterfaceC alloc] init];

        // allocate feature4 instance
        if (builder.feature4)
            self.feature4 = builder.feature4;
        else
            self.feature4 = [[InterfaceD alloc] init];

        // allocate feature5 instance
        if (builder.feature5)
            self.feature5 = builder.feature5;
        else
            self.feature5 = [[InterfaceE alloc] init];

        // allocate feature6 instance
        if (builder.feature6)
            self.feature6 = builder.feature6;
        else
            self.feature6 = [[InterfaceF alloc] init];

        // allocate feature7 instance
        if (builder.feature7)
            self.feature7 = builder.feature7;
        else
            self.feature7 = [[InterfaceG alloc] init];

        // allocate feature8 instance
        if (builder.feature8)
            self.feature8 = builder.feature8;
        else
            self.feature8 = [[InterfaceH alloc] init];

 return self;
}

Upvotes: 1

Views: 117

Answers (1)

Amin Negm-Awad
Amin Negm-Awad

Reputation: 16650

Assuming that I understood you correctly (Doesn't any answer assume this?), it seems to be easier in Objective-C.

First of all yo do not need a separate class LibraryBuilder. Simply use a dictionary.

Secondly you can take advantage out of KVC and the dynamic binding of Objective-C.

A. Create a look-up for all features, i. e. in a class method (or +initialize or whatever)

+(NSDictionary*)defaultFeatureClasses
{
  return
  @{
    @"featureName1" : [FeatureClass1 class],
    @"featureName2" : [FeatureClass2 class],
    // …
    @"featureNameN" : [FeatureClassN class],
  };
});

If the class name correspondents to the feature name, one could simplify that, but this does not look like a big deal.

Next, write an initializer, that takes a feature dictionary:

- (instancetype)initWithFeatureSet:(NSDictionary*)features
{
  self = [super init];
  if (self)
  {
    [defaultClasses enumerateKeysAndObjectsUsingBlock:
    ^(NSString *key, Class featureClass, BOOL *stop)
    {
      id feature = features[key];
      if (feature==nil)
      {
        feature = [featureClass new];
      }
      [self setValue:feature forKey:key];
    }

  }
  return self;
}

Then you can easily set-up a library

 … [[Library alloc] initWithFeatures:
 @{
   @"Feature1" : myFeaatureA,
   @"Feature2" : anotherCustomFeature,
 }];

To expand the features you only have to change a single line in a single method.

Upvotes: 1

Related Questions