Objective-C and Cluster Class

I read some information about the class cluster template and understood the following:

  • the public cluster class provides an interface without a real implementation; other classes implement it for different cases;

  • has some similarities with the abstract Factory pattern: when we call the +classNameWith... method, depending on the arguments, it can select the most suitable subclass and return it.

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

But what I did not understand: how -init... works -init... method of the open class of the cluster: [[NSNumber alloc] initWithDouble:1.0] , because after calling alloc it already allocates an instance of NSNumber , and not its subclass.

So, can anyone explain how the alloc-init method of a public cluster class actually works, and when does a particular subclass be created and returned?

+7
design-patterns ios objective-c
source share
2 answers

Basically, the instance you allocated can be thrown away and replaced with another instance. Technically, this does not apply to class clusters, so when you call super in any init method, you need to set the result as self :

 self = [super init]; 
+5
source share

Here is the abstract factory implementation for Objective C.

 // Usage BrandingFactory * factory = [BrandingFactory factory:Sierra]; UIView * view = [factory brandedView]; UIButton * button = [factory brandedMainButton]; UIToolbar * toolbar = [factory brandedToolbar]; ____________________________________________ // BrandingFactory.h // AbstractFactory #import <Foundation/Foundation.h> typedef enum ouputTypes { Acme, Sierra } OutputTypes; @interface BrandingFactory : NSObject { } + (BrandingFactory *) factory: (OutputTypes)type; - (UIView *) brandedView; - (UIButton *) brandedMainButton; - (UIToolbar *) brandedToolbar; @end ___________________________________________________ // BrandingFactory.m // AbstractFactory #import "BrandingFactory.h" #import "AcmeBrandingFactory.h" #import "SierraBrandingFactory.h" @implementation BrandingFactory + (BrandingFactory *) factory:(OutputTypes)type { if (type == Sierra) { return [[[SierraBrandingFactory alloc] init] autorelease]; } else if (type == Acme) { return [[[AcmeBrandingFactory alloc] init] autorelease]; } return nil; } - (UIView *) brandedView { return nil; } - (UIButton *) brandedMainButton { return nil; } - (UIToolbar *) brandedToolbar { return nil; } @end ________________________________________ // SierraBrandingFactory.h // AbstractFactory #import <Foundation/Foundation.h> #import "BrandingFactory.h" @interface SierraBrandingFactory : BrandingFactory { } - (UIView*) brandedView; - (UIButton*) brandedMainButton; - (UIToolbar*) brandedToolbar; @end // SierraBrandingFactory.m // AbstractFactory #import "SierraBrandingFactory.h" #import "SierraView.h" #import "SierraMainButton.h" #import "SierraToolbar.h" @implementation SierraBrandingFactory - (UIView*) brandedView { // returns a custom view for Sierra return [[[SierraView alloc] init] autorelease]; } - (UIButton*) brandedMainButton { // returns a custom main button for Sierra return [[[SierraMainButton alloc] init] autorelease]; } - (UIToolbar*) brandedToolbar { // returns a custom toolbar for Sierra return [[[SierraToolbar alloc] init] autorelease]; } @end ________________________________________ // AcmeBrandingFactory.h // AbstractFactory #import <Foundation/Foundation.h> #import "BrandingFactory.h" @interface AcmeBrandingFactory : BrandingFactory { } - (UIView *) brandedView; - (UIButton *) brandedMainButton; - (UIToolbar *) brandedToolbar; @end // AcmeBrandingFactory.m // AbstractFactory #import "AcmeBrandingFactory.h" #import "AcmeView.h" #import "AcmeMainButton.h" #import "AcmeToolbar.h" @implementation AcmeBrandingFactory - (UIView *) brandedView { // returns a custom view for Acme return [[[AcmeView alloc] init] autorelease]; } - (UIButton *) brandedMainButton { // returns a custom main button for Acme return [[[AcmeMainButton alloc] init] autorelease]; } - (UIToolbar *) brandedToolbar { // returns a custom toolbar for Acme return [[[AcmeToolbar alloc] init] autorelease]; } @end 
-2
source share

All Articles