1

我想创建一个“多级”类集群,这样每个“具体”类可以在满足某些条件时返回一个更具体的实例。

例如在基类中:

@interface BaseClass : NSObject  // this is the public API of the service

+(instancetype)initWithData:(Data *)data;
// common interface of all the specific implementations..
...
...

@end


@implementation BaseClass

// basically implementing the "cluster"
+(instancetype)initWithData:(Data *)data {
  // testing some conditions to decide on the more specific version of the class to return...
  if (data.condition == condition1) {
     return [[SomeClassOne alloc] initWithData:data];
  }
  if(data.condition == condition2) {
     return [[SomeClassTwo alloc] initWithData:data];
  }
  ... 
  // have many types which could be returned 
}


// an example of a specific instance that should be returned from the cluster - all of these classes are "private" implementations of the base class
@implementation SomeClassOne

-(instancetype)initWithData:(Data *)data {
  self = [super initWithData:data];
  // all was good until a new optimization came about...
  // now this instance can refine the class cluster even better
  // what I would want is a way to do:
  self = [[SomeClassOne_EvenBetterVersion alloc] initWithData:data];
  // but this would be bad - and would cause recursion if the new version inherits this version...
}
@end

我不想在基类中不断添加新条件(大的“if”语句),因为条件变得非常特定于具体类 - 主要是与新功能有关的优化。

有没有更好的模式呢?

我考虑在每个子类中创建一个类方法来进行额外的检查——但这变得非常尴尬,必须在每个子类中调用 [Subclass initWithData:data]

4

1 回答 1

1

一些可能会改善这一点的事情:

1) 覆盖 allocWithZone:让您的 BaseClass 返回 BaseClass 的单例实例。这可以防止额外的对象分配。然后 BaseClass 的 initWithData: 方法将返回真实的实例。

2) 以这样的方式构造或转换 Data 参数,以便于字典查找以获取您的具体实现类。在 initWithData 中实例化它:

在 +initialize 中静态创建一个结构(或随时动态),如下所示:

static_dictionary = @{ @"Some string or hashable condition" : [CoolSubclass class],
                       @"Condition2" : [CoolerSubclass class] };

现在在 initWithData: 你可以不断地查找你的类型和干净的代码。

Class my_type = [static_dictionary objectForKey: transformated_data_condition];
if(my_type == Nil) { // throw? return nil? }
return [[my_type alloc] initWithData: data];

现在重复这个过程变得很容易——也许 CoolerSubclass 是另一个类集群,它有自己的类型字典和测试方法。

于 2015-09-10T16:53:27.387 回答