View Code
@interface BrandingFactory : NSObject
{
}
+ (BrandingFactory *) factory;
- (UIView *) brandedView;
- (UIButton *) brandedMainButton;
- (UIToolbar *) brandedToolbar;
@end
View Code
#define USE_ACME
@implementation BrandingFactory
+ (BrandingFactory *) factory
{
#if defined (USE_ACME)
return [[[AcmeBrandingFactory alloc] init] autorelease];
#elif defined (USE_SIERRA)
return [[[SierraBrandingFactory alloc] init] autorelease];
#else
return nil;
#endif
}
- (UIView *) brandedView
{
return nil;
}
- (UIButton *) brandedMainButton
{
return nil;
}
- (UIToolbar *) brandedToolbar
{
return nil;
}
@end
=============================
View Code
@interface SierraBrandingFactory : BrandingFactory
{
}
- (UIView*) brandedView;
- (UIButton*) brandedMainButton;
- (UIToolbar*) brandedToolbar;
View Code
@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
=============================
View Code
@interface AcmeBrandingFactory : BrandingFactory
{
}
- (UIView *) brandedView;
- (UIButton *) brandedMainButton;
- (UIToolbar *) brandedToolbar;
View Code
@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
=============================
用戶端:
View Code
BrandingFactory * factory = [BrandingFactory factory];
UIView * view = [factory brandedView];
UIButton * button = [factory brandedMainButton];
UIToolbar * toolbar = [factory brandedToolbar];