Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C and Class Cluster pattern

I have read some information about Class Cluster pattern, and understood next:

  • public cluster class only provides interface without actual implementation, other classes implement it for different cases;

  • it has some similarities with Abstract Factory pattern: when we call method +classNameWith... it depending on arguments can choose the most appropriate subclass and return it.

For example, +[NSNumber numberWithDouble:1.0], will return implementation for storing double values.

But what i didn't understand: how works -init... methods of public cluster class: [[NSNumber alloc] initWithDouble:1.0], as after calling alloc it already allocates instance of NSNumber, not it's subclass.

So, can somebody explain how actually works alloc-init methods of public cluster class, and when concrete subclass instantiated and returned?

like image 445
Mikhail Avatar asked Jul 27 '13 23:07

Mikhail


1 Answers

Basically, the instance you have allocated could be thrown away and replaced with a different instance. Technically, this isn't specific to class clusters and that is why when you call super in any init method you need to set the result as self:

self = [super init];
like image 122
Wain Avatar answered Sep 17 '22 13:09

Wain