0

I'm studying some ObjC source here and one of the controllers has a property of type NSDictionary that is called "overrideClassNames". As key it wants the class of an existing interface and as value the class of a subclass of the existing one, like:

controller.overrideClassNames = @{(id)[DefaultType class] : [MySubClassedDefaultType class]};

Whenever an instance of DefaultType is required, it would look up the dictionary and create a more specialized instance if one has been set.

I'm thinking if this is actually a common approach in ObjC? Coming from C# and Java, my idea would have been to create a delegate factory method that is called if an instance of DefaultType is required. The user of the class could then return his more specialized version. Alternatively (in C#) I would make the class generic and let it have a <T> : DefaultType, new()

4

1 回答 1

1

这主要与工厂模式无关,而是与 IMO 的子类化有关——但是

是的。拥有一个返回类对象的方法是一种常见的方法 - 在 osx 中例如。一个控件有一个 CellClass(它分配/初始化来为它绘制)

在单身人士中,您通常会遇到以下情况:

_sharedInstance = [[[self class] alloc] init];
于 2012-11-30T20:53:50.013 回答