C和类集群模式
我已经阅读了关于类集群模式的一些信息,并且接下来会理解:
公共集群类只提供没有实际实现的接口,其他类实现不同的情况;
它与Abstract Factory模式有一些相似之处:当我们调用method +classNameWith...
它依赖于参数可以选择最合适的子类并返回它。
例如, +[NSNumber numberWithDouble:1.0]
,将返回存储双+[NSNumber numberWithDouble:1.0]
值的实现。
但我不明白:如何工作-init...
公共集群类的方法: [[NSNumber alloc] initWithDouble:1.0]
,因为在调用alloc
它已经分配了NSNumber
实例,而不是它的子类。
那么,有人可以解释公共集群类的alloc-init
方法实际上是如何工作的,以及何时具体的子类实例化并返回?
基本上,您分配的实例可能会被丢弃,并替换为其他实例。 从技术上讲,这不是特定于类集群的,这就是为什么当你在任何init
方法中调用super
,你需要将结果设置为self
:
self = [super init];
这是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
链接地址: http://www.djcxy.com/p/14905.html
上一篇: C and Class Cluster pattern
下一篇: Push to GitLab repository within CI server (deploy keys)