2013-07-27 1 views
7

나는 클래스 클러스터 패턴에 대한 정보를 읽고, 다음 이해 :목표 - C와 클래스 클러스터 패턴

  • 공공 클러스터 클래스는 다른 클래스가 서로 다른 경우에 그것을 구현, 실제 구현하지 않고 인터페이스를 제공합니다;

  • 추상 팩토리 패턴과 몇 가지 유사점이 있습니다. +classNameWith... 메서드를 호출하면 인수에 따라 가장 적합한 하위 클래스를 선택하고 반환 할 수 있습니다.

예를 들어 +[NSNumber numberWithDouble:1.0]은 이중 값을 저장하기위한 구현을 반환합니다.

하지만 난 이해하지 못했다 무엇 : 공공 클러스터 클래스의 -init... 방법을 작동하는 방법 : alloc를 호출 한 후 같은 [[NSNumber alloc] initWithDouble:1.0]을 이미되지는 하위 클래스, NSNumber의 인스턴스를 할당합니다.

그래서 공개 클러스터 클래스의 alloc-init 메소드가 실제로 어떻게 작동하는지 설명 할 수 있습니까? 구체 서브 클래스가 인스턴스화되어 리턴 될 때?

+2

[NSNumber.m의 GNUStep 버전] (https://github.com/gnustep/gnustep-base/blob/master/Source/NSNumber.m)을 살펴볼 수 있습니다. –

+0

Josh Caswell, 훌륭한 링크에 감사드립니다! – Mikhail

+0

[Objective-C Programming : Class Clusters의 개념] (https://developer.apple.com/library/ios/documentation/general/conceptual/CocoaEncyclopedia/ClassClusters/ClassClusters.html)을 이미 읽었다 고 가정합니다. –

답변

5

기본적으로 할당 한 인스턴스는 버리고 다른 인스턴스로 바꿀 수 있습니다. 기술적으로,이 클래스 클러스터를 특정하지 않고 당신이 어떤 init 방법 super를 호출 할 때 self로 결과를 설정해야하는 이유는 다음과 같습니다

여기
self = [super init]; 
+1

또한 + alloc 메서드는 다른 메서드와 마찬가지로 재정의 할 수 있습니다. 클래스 클러스터 alloc은 새 인스턴스를 요청할 때마다 생성되고 해제되는 대신 새 인스턴스가 아닌 정적 인스턴스를 반환합니다. –

+0

지루한 우주 비행사, 그렇습니다. 그렇지 않으면 할당 된 객체를 파괴하기 위해 약간의 오버 헤드가 생기기 때문에 alloc 메소드를 오버라이드한다고 생각합니다. – Mikhail

-2

이 목적 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 
+0

OP가 지적했듯이. 두 패턴은 비슷하지만 동일하지 않습니다 –

+0

그래서 이것은 추상 팩토리 구현입니다. 하지만 일반적으로 그들은 매우 유사합니다. http://stackoverflow.com/a/2459385/1847511 –

+0

몇 마디로 그 차이를 설명해 주시겠습니까? –