簡體   English   中英

Objective-C和Class Cluster模式

[英]Objective-C and Class Cluster pattern

我已經閱讀了有關類集群模式的一些信息,並且接下來會理解:

  • 公共集群類只提供沒有實際實現的接口,其他類為不同的情況實現它;

  • 它與Abstract Factory模式有一些相似之處:當我們調用method +classNameWith...它依賴於參數可以選擇最合適的子類並返回它。

例如, +[NSNumber numberWithDouble:1.0]將返回存儲double值的實現。

但是我不明白:如何工作-init...公共集群類的方法: [[NSNumber alloc] initWithDouble:1.0] ,因為在調用alloc它已經分配了NSNumber實例,而不是它的子類。

那么,有人可以解釋實際上如何工作公共集群類的alloc-init方法,以及具體的子類實例化和返回時?

基本上,您分配的實例可能會被丟棄並替換為不同的實例。 從技術上講,這不是特定於類集群的,這就是為什么當你在任何init方法中調用super ,你需要將結果設置為self

self = [super init];

這是Objective C的Abstract工廠實現。

// 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM