简体   繁体   English

Objective-C和Class Cluster模式

[英]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. 它与Abstract Factory模式有一些相似之处:当我们调用method +classNameWith...它依赖于参数可以选择最合适的子类并返回它。

For example, +[NSNumber numberWithDouble:1.0] , will return implementation for storing double values. 例如, +[NSNumber numberWithDouble:1.0]将返回存储double值的实现。

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. 但是我不明白:如何工作-init...公共集群类的方法: [[NSNumber alloc] initWithDouble:1.0] ,因为在调用alloc它已经分配了NSNumber实例,而不是它的子类。

So, can somebody explain how actually works alloc-init methods of public cluster class, and when concrete subclass instantiated and returned? 那么,有人可以解释实际上如何工作公共集群类的alloc-init方法,以及具体的子类实例化和返回时?

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 : 从技术上讲,这不是特定于类集群的,这就是为什么当你在任何init方法中调用super ,你需要将结果设置为self

self = [super init];

Here is the Abstract factory implementation for Objective C. 这是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