我想创建一个“多级”类集群,这样每个“具体”类可以在满足某些条件时返回一个更具体的实例。
例如在基类中:
@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]